SwiftUI基础控件之NavigationView、Present

1、导航跳转

  • 页面A

import SwiftUI

struct ContentViewA: View {
    
    @State var isNavPush = false
    
    var body: some View {
        NavigationView {
            VStack {
                NavigationLink(isActive: $isNavPush) {
                    NavPushViewB()
                } label: {
                }

                Button {
                    isNavPush = true
                } label: {
                    Text("导航跳转")
                }
            }.navigationTitle("页面A")
            .padding()
        }

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentViewA()
    }
}
  • 页面B
import SwiftUI

struct NavPushViewB: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .imageScale(.large)
                .foregroundColor(.accentColor)
            Text("Hello, world!")
        }
        .padding()
    }
}

struct NavPushView_Previews: PreviewProvider {
    static var previews: some View {
        NavPushViewB()
    }
}

2、导航设置

//  以上一份代码的页面“NavPushViewB”为例,来设置导航

import SwiftUI

struct NavPushViewB: View {
    var body: some View {
        VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Hello, world!")
            }.navigationTitle("页面B")
            .padding()
    }
}

struct NavPushView_Previews: PreviewProvider {
    static var previews: some View {
        NavPushViewB()
    }
}

自定义效果如下

import SwiftUI

struct NavPushViewB: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            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: {
                Image("nav_back_black") //导航返回按钮图标
            }))
            .padding()
        }
    }
}

struct NavPushView_Previews: PreviewProvider {
    static var previews: some View {
        NavPushViewB()
    }
}

3、Present跳转(模态跳转

import SwiftUI

struct ContentViewA: View {
    
    @State var isNavPush = false
    @State var isPresent = false
    
    var body: some View {
        NavigationView {
            VStack {
                
                NavigationLink(isActive: $isNavPush) {
                    NavPushViewB()
                } label: {
                    
                }
                
                
                Button {
                    isNavPush = true
                } label: {
                    Text("导航跳转")
                }.padding(.bottom, 30)
                
                
                Button {
                    isPresent = true
                } label: {
                    Text("Present跳转")
                }
                
                
            }.navigationTitle("页面A")
                .sheet(isPresented: $isPresent, content: {// 模态跳转(未满屏)
                    PresentB()
                })
                .padding()
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentViewA()
    }
}

跳转页面全屏展示

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

如果需要再模态跳转的“PresentB”页面加导航,直接加即可:

import SwiftUI

struct PresentB: View {
    
    @Environment(\.presentationMode) var presentationMode
    
    var body: some View {
        NavigationView {
            VStack {
                Image(systemName: "globe")
                    .imageScale(.large)
                    .foregroundColor(.accentColor)
                Text("Hello, world!")
            }.navigationBarTitle("PresentB", displayMode: .inline) //设置标题displayMode,默认的是:automatic(大标题)
                .navigationBarBackButtonHidden(true) //隐藏系统的导航返回按钮
                .navigationBarItems(leading: Button(action: { //自定义导航的返回按钮
                    presentationMode.wrappedValue.dismiss() //返回上级页面
                }, label: {
                    Image("nav_back_black") //导航返回按钮图标
                }))
                .padding()
        }
    }
}

struct PresentB_Previews: PreviewProvider {
    static var previews: some View {
        PresentB()
    }
}

以上就是常用的两种页面跳转和导航栏的设置了,在常规项目的开发中,我们会遇到两个问题:

1、下导航tabbar所持有的页面(比如:“我的”页面)跳转到其他页面(比如:“个人资料”页面)的时候,下导航tabbar未隐藏(“个人资料”页面依然展示这下导航tabbar)的问题。
答:将NavigationView套在tabbar外层即可。
2、跳转页面后如何返回到指定的页面?(如:A->B->C->D,如何在D页面直接返回到B页面?或者返回到A页面?)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容