初学 SwiftUI,遇到点小问题,想请教一下大家。我有这样几个 View:
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: ListView()) {
Text("To ListView")
}
}
}
}
struct ListView: View {
var body: some View {
NavigationLink(destination: DetailView()) {
Text("To DetailView")
}
}
}
struct DetailView: View {
var body: some View {
Text("DetailView")
}
}
现在情况是在 iPhone 上,如果在 DetailView
中从 Portrait 转换到 Landscape,就会自动回到 ListView
,如果先横屏再进入 DetailView
,之后怎么旋转也不会回去。
搜了一下发现给 NavigationView
设置 StackNavigationViewStyle
就不会出现这种情况了,但是如果我想在默认或者两栏样式的 NavigationView
上旋转屏幕而不自动返回 ListView
该怎么做呢?
1
95276 2021-02-08 00:44:53 +08:00 1
首先建议你在 iPhone 上使用 StackNavigationViewStyle
其次下面的代码应该能达到你想要的效果,但不建议使用 ```swift import SwiftUI struct ContentView: View { var body: some View { VStack { NavigationView { NavigationLink(destination: ListView()) { Text("To ListView") } } } } } struct ListView: View { @State var tap = false @State var active = false var body: some View { NavigationLink(destination: DetailaView(tap: $tap), isActive: $active) { Text("To DetailView") .onTapGesture { active = true tap = true } } .onAppear { active = tap } } } struct DetailaView: View { @Binding var tap: Bool var body: some View { Text("DetailView") .onDisappear { tap = false } } } ``` |
2
shoujiaxin OP @95276 学习了,感谢
|