SwiftUI 和 Mac 催化剂 : Sidebar is not displayed correctly

标签 swift swiftui sidebar catalyst mac-catalyst

我启用了 Mac Catalyst对于 iPad App并遇到了一个奇怪的Display ProblemSidebar .
Screenshot of the Mac Catalyst App
代码:

@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 SidebariPad但是,正如您所见,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/

相关文章:

SwiftUI:ObservableObject 在重绘时不会保持其状态

CSS 移动 div 问题

swift - 如何在不使用旧 UISegmentedControl 的情况下更改 swiftUI 中分段控件的半径

sidebar - offcanvas-sidebar bootstrap 4 粘性侧边栏

php - 如何在 Wordpress 中格式化发布日期?

ios - 点击链接后 UIWebView 停止触发委托(delegate)方法

swift - Swift Playground 中的数据值变化

ios - 在 TableView 中显示搜索结果时没有结果 - swift

ios - Swift 在两个 View Controller 上使用后退按钮

xcode - 断言 UITest 中的按钮上存在图像