使用官方提供的不管是什么系统,页面跳转都是必须的
本文总结了个人在学习SwiftUI
中遇到的几种页面跳转实现方式
NavigationView
官方提供的方式可以在跳转页面后,自动添加返回按钮
但官方的方式个人觉得很不灵活
第一个画面(即将跳出的画面)
struct PersonUIView: View {
var body: some View {
NavigationView(content: {
NavigationLink(
destination: PersonSecondUIView(),
label: {
MyCustomTitleText(text: "点我跳转到第二个画面")
})
})
.navigationBarTitle(Text("Person"),displayMode: .inline)
}
}
struct PersonUIView_Previews: PreviewProvider {
static var previews: some View {
PersonUIView()
}
}
第二个画面(即将跳入的画面)
struct PersonSecondUIView: View {
var body: some View {
Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
}
}
struct PersonSecondUIView_Previews: PreviewProvider {
static var previews: some View {
PersonSecondUIView()
}
}
直接自定义跳转方法
用法灵活,但挑战后要想返回原画面需要自己添加方法
import SwiftUI
struct NotifyUIView: View {
var body: some View {
NavigationView{
ScrollView(.vertical, showsIndicators: true, content: {
VStack(alignment: .leading, content: {
Button(action: goToHomeSecond, label: {
Text("不使用NavigationLink页面跳转")
})
Spacer()
})
})
}
}
func goToHomeSecond(){
if let window = UIApplication.shared.windows.first
{
window.rootViewController = UIHostingController(rootView: HomeSecondUIView())
window.makeKeyAndVisible()
}
}
}
struct NotifyUIView_Previews: PreviewProvider {
static var previews: some View {
NotifyUIView()
}
}
//返回
@Environment(\.presentationMode) var presentationMode
Button("Dismiss") {
self.presentationMode.wrappedValue.dismiss()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)