SWiftUI中使用NavigationLink
来进行show detail,push和pop
首先看下show detail
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink("Tap me", destination: DetailView())
.navigationBarTitle("Master")
}
}
}
struct DetailView: View {
var body: some View {
Text("This is detail view")
.navigationBarTitle("Detail")
}
}
在Master-Detail 模式下 NavigationLink默认就是 showDetail
如果我们想从Detail push 到下一个页面,需要手动调用isDetailLink()
api, 代码如下
struct DetailView: View {
var body: some View {
VStack {
Text("This is detail view")
NavigationLink("Tap me to next page", destination: DetailChildView()).isDetailLink(false)
}
.navigationBarTitle("Detail")
}
}
struct DetailChildView: View {
var body: some View {
Text("This is detail view")
.navigationBarTitle("This is detail view")
}
}
这里要注意的是必须要手动调isDetailLink(false)
, 文档提示默认是true
/// Sets whether or not the `NavigationLink` should present its destination /// as the "detail" component of the containing `NavigationView`. /// /// If not set, defaults to `true`. public func isDetailLink(_ isDetailLink: Bool) -> some View
如果调用这个api,那么默认是showDetail,当前的DetailView
会被直接替换成DetailChildView
而不是Push,并且没有动画,也无法返回
网友评论