我启用了 Mac Catalyst
对于 iPad App
并遇到了一个奇怪的Display Problem
的Sidebar
.
代码:
@State private var selection: NavigationItem? = .start
NavigationView {
List(selection: $selection) {
NavigationLink(destination: StartView(), tag: NavigationItem.start, selection: $selection) {
Label("Start", systemImage: "square.grid.2x2.fill")
.accessibility(label: Text("Start"))
}
.tag(NavigationItem.start)
// 4 more Items
}
.listStyle(SidebarListStyle())
.navigationBarTitle("Impfpass+")
StartView()
}
问题:此代码生成 Standard Sidebar
在 iPad
但是,正如您所见,Mac Version
这个看起来很奇怪angular design
.我怎样才能实现Standard macOS Sidebar Design
?
最佳答案
我遇到了同样的问题。
原来内置的 SidebarListStyle 在 macOS 上表现不正确。
Here是 Steven Troughton-Smith 建议的解决方案,它意味着将 SwiftUI View 包装在 UISplitViewController 中。
基本上 :
struct SidebarSplitView: View, UIViewControllerRepresentable {
typealias UIViewControllerType = UISplitViewController
let splitViewController = UISplitViewController(style: .doubleColumn)
var columnA = UIViewController()
var columnB = UIViewController()
init<A:View, B:View>(@ViewBuilder content: @escaping () -> TupleView <(A,B)>) {
let content = content()
columnA = UIHostingController(rootView: content.value.0)
columnB = UIHostingController(rootView: content.value.1)
columnA.view.backgroundColor = .clear
columnB.view.backgroundColor = .clear
splitViewController.viewControllers = [columnA, columnB]
}
func makeUIViewController(context: Context) -> UIViewControllerType {
splitViewController.primaryBackgroundStyle = .sidebar
return splitViewController
}
func updateUIViewController(_ uiView: UIViewControllerType, context: Context) { }
}
然后你就可以称之为:struct ContentView: View {
var body: some View {
SidebarSplitView {
Sidebar() // here goes your sidebar
MainView() // here your main view
}
}
}
关于SwiftUI 和 Mac 催化剂 : Sidebar is not displayed correctly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65749388/