如何在 iPad 上的横向模式下从 NavigationSplitView 中删除侧边栏切换,但保持纵向模式?类似于 iOS 17 中家庭应用程序的工作方式。
到目前为止我已经有了这个,但在第一次启动时在切换方向一次之前它无法正常工作:
struct SidebarMainView: View {
@Environment(\.horizontalSizeClass) private var horizontalSizeClass
@State private var isPortrait = true
var body: some View {
NavigationSplitView {
List {
Text("1")
Text("2")
Text("3")
}
.listStyle(SidebarListStyle()).toolbar(removing: isPortrait ? .none : .sidebarToggle).navigationTitle("HP").onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
guard let scene = UIApplication.shared.windows.first?.windowScene else { return }
self.isPortrait = scene.interfaceOrientation.isPortrait
}
} detail: {
Text("Select a department: \(self.isPortrait ? "yes" : "no")")
}
}
}
作为参考,这是家庭应用程序在不同布局下的样子。请注意每个布局中的强制侧边栏和可选侧边栏。我也想实现同样的目标。
如果
windowWidth < WindowHieght
那么use navigation Tab Bar
否则use side menu navigation
就这么简单