• SwiftUI 导航设置


    一、导航跳转

    • 页面A
    import SwiftUI
    
    struct NavJumpAView: View {
        
        @State var isNavPush = false
        
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(isActive: $isNavPush) {
                        NavJumpBView()
                    } label: {
                    }
                    
                    Button {
                        isNavPush = true
                    } label: {
                        Text("导航跳转")
                    }
                }
                .navigationBarTitle("页面A")
                    .padding()
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 页面B
    
    struct NavJumpBView: View {
        
        @State var isNavPush = false
        
        var body: some View {
            NavigationView {
                VStack {
                    Image(systemName: "globe")
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                    Text("Hello, world!")
                }
                .padding()
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    二、导航设置

    struct NavJumpBView: View {
        @Environment(\.presentationMode) var presentationMode
        
        var body: some View {
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Hello, world!")
            }
            .navigationBarTitle("页面B", displayMode: .inline) // 设置标题displayMode,默认的是:automatic(大标题)
            .navigationBarBackButtonHidden(true) // 隐藏系统的导航返回按钮
            .navigationBarItems(leading: Button(action: {// 自定义导航的返回按钮
                presentationMode.wrappedValue.dismiss() // 返回上级页面
            }, label: {
                if let resBundlePath = Bundle.main.path(forResource: "CommonResource", ofType: "bundle"),
                   let resBundle = Bundle(path: resBundlePath) {
                    Image("arrow_left_dark@2x", bundle: resBundle).background(Color.red) // 导航返回按钮图标
                    Text("Back")
                } else {
                    Text("Back")
                }
            }))
            .padding()
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    三、Present跳转(模态跳转)

    
    struct NavJumpAView: View {
        
        @Environment(\.presentationMode) var presentationMode
        
        @State var isNavPush = false
        @State var isPresent = false
        
        var body: some View {
            NavigationView {
                VStack {
                    NavigationLink(isActive: $isNavPush) {
                        NavJumpBView()
                    } label: {
                    }
                    
                    Button {
                        isNavPush = true
                    } label: {
                        Text("导航跳转")
                    }
                    Spacer(minLength: 5).frame(height: 15)
                    Button {
                        isPresent = true
                    } label: {
                        Text("模态跳转")
                    }
                }
                .navigationBarTitle("页面A", displayMode: .inline)
                .sheet(isPresented: $isPresent) {
                    NavJumpBView()
                }
                .navigationBarItems(leading: Button(action: {
                    presentationMode.wrappedValue.dismiss()
                }, label: {
                    Text("Back").foregroundColor(.black)
                }))
                    .padding()
                    
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    跳转全屏

    // 只需要将上面的 “ sheet”替换成“ fullScreenCover”的方式就可以了
    .fullScreenCover(isPresented: $isPresent, content: {
          PresentB()
      })
    
    • 1
    • 2
    • 3
    • 4

    四、返回页面

    @Environment(\.presentationMode) var presentationMode
    
    presentationMode.wrappedValue.dismiss() // 返回上级页面
    
    • 1
    • 2
    • 3
  • 相关阅读:
    Git安装
    MySQL/Oracle字符串分割
    合宙ESP32C3 的Arduino开发教程环境配置
    C++:AVL树
    LCD1602指定位置显示字符串-详细版
    Codeforces Round #815 (Div. 2)
    八、开发者工具与单元测试
    层次聚类分析
    [PyTorch][chapter 61][强化学习-免模型学习 off-policy]
    Google Earth Engine——使用geetool批量下载单景影像以Landsat 8 反演后的NDSI结果
  • 原文地址:https://blog.csdn.net/guoxulieying/article/details/132970715