ios 如何确定有多少“NavigationSplitView”列是可见的?

ssm49v7z  于 2022-12-15  发布在  iOS
关注(0)|答案(1)|浏览(102)

概述

  • 我使用的是NavigationSplitView,可见列数可能会根据设备(macOS、iPhone、iPad)以及是否使用分屏(iPad可以同时运行2个应用程序)而有所不同。

问题

1.在NavigationSplitView中,如何确定有多少列是可见的?
1.或者如何确定视图是否被推送?
1.或者视图是一个幻灯片(iPad侧边栏幻灯片)?

背景

提问理由:我有一个侧边栏列表、内容列表和详细列表。现在,我希望根据可见列的数量选择不同的单元格

样品代码:

struct ContentView: View {
    @State private var departments = ["D1"]
    @State private var employees = ["E1", "E2", "E3"]
    @State private var selectedDepartment: String?
    @State private var selectedEmployee: String?

    var body: some View {
        NavigationSplitView {
            List(departments, id: \.self, selection: $selectedDepartment) { department in
                Text(department)
            }
        } content: {
            List(employees, id: \.self, selection: $selectedEmployee) { employee in
                Text(employee)
            }
        } detail: {
            if let selectedEmployee {
                Text(selectedEmployee)
            } else {
                Text("No employee selected")
            }
        }
    }
}
qyswt5oh

qyswt5oh1#

您可以在初始化程序中使用NavigationSplitViewVisibility结构。

@State private var columnVisibilty: NavigationSplitViewVisibility = .all

这将是一个3列设计的示例。通过阅读或更改状态变量,您可以控制它。(因此,绑定,系统设置它当前显示的内容)添加以便于说明

NavigationSplitView(columnVisibility: $columnVisibilty) sidebar: {
...
} content: {
...
} detail: {
...
}

下面是苹果文档的链接:https://developer.apple.com/documentation/swiftui/navigationsplitviewvisibility
添加了粗略示例:

struct ContentView: View {
    @State private var columnVisibility: NavigationSplitViewVisibility = .all
    var body: some View {
        NavigationSplitView(columnVisibility: $columnVisibility) {
            Text("Sidebar:")
                .toolbar {
                    Button("Print") {
                        print(columnVisibility)
                    }
                }
        } content: {
            Text("Content:")
                .toolbar {
                    Button("Print") {
                        print(columnVisibility)
                    }
                }
        } detail: {
            Text("Detail:")
                .toolbar {
                    Button("Print") {
                        print(columnVisibility)
                    }
                }
        }
        
    }
}

这可以让我设置一个初始设置,但它会随着SplitView的当前状态而更新。只需按下打印按钮,您就可以看到更改。

相关问题