使用以下代码,在纵向模式下为iPads启动应用程序时会显示Useless View
。按一下Back (后退)按钮,细节视图显示"Link 1 destination“(链接1目的地)。只有在第二次按下时才会显示侧边栏。这不是我们想要的任何应用程序的行为。
如果我们从代码中删除无用的视图,我们在启动时会得到一个空白屏幕,我们仍然需要按一次后退按钮才能到达链接1的目的地,两次才能到达侧边栏。此外,侧边栏中列表的样式似乎不太可取。
我们想要的行为是在应用程序启动时显示Link 1 destination,然后单击Back按钮将我们带到侧边栏。在SwiftUI 3中,这是我们期望的任何应用程序的完全标准行为吗?
struct ContentView: View {
@State var selection: Int? = 0
var body: some View {
NavigationView {
List {
NavigationLink("Link 1", tag: 0, selection: $selection) {
Text("Link 1 destination")
}
NavigationLink("Link 2", tag: 1, selection: $selection) {
Text("Link 2 destination")
}
}
//If we delete the Useless View, we still have to press Back twice
//to get to the sidebar nav.
Text("Useless View")
.padding()
}
}
}
发布于 2021-10-26 09:01:02
@Yrb comment pointed to right track,对底层UIKit UISplitViewController
进行修补,以强制显示主列。然而,在细节中有一些沉重的视图,在较慢的iPads上出现和消失的主列有一些闪烁。所以我用Introspect library做了这个
struct ContentView: View {
@State var selection: Int? = 0
var body: some View {
NavigationView {
List {
NavigationLink("Link 1", tag: 0, selection: $selection) {
Text("Link 1 destination")
}
NavigationLink("Link 2", tag: 1, selection: $selection) {
Text("Link 2 destination")
}
}
//In my case I never have a 'Nothing Selected' view, you could replace EmptyView() here
//with a real view if needed.
EmptyView()
}
.introspectSplitViewController { svc in
if isPortrait { //If it's done in landscape, the primary will be incorrectly disappeared
svc.show(.primary)
svc.hide(.primary)
}
}
}
}
这正如我们所预期的那样呈现,没有闪烁或其他伪像。
https://stackoverflow.com/questions/69715797
复制