在SwiftUI

时间:2019-10-22 22:52:13

标签: ios swift macos swiftui

我一直在尝试为macOS构建一个工具栏-tabview组件,该组件可以与子视图组成,就像下面的TabView:

struct ContentView: View {    
    var body: some View {
        TabView {
            Text("First View")
                .tabItem {
                    Image(name: "NSUserAccounts")
                    Text("First")
                }.tag(0)
            Text("Second View")
                .tabItem {
                    Image(name: "NSUserAccounts")
                    Text("Second")
                }.tag(1)
        }
    }
} 

此刻我有这样的东西:

struct ToolbarTabView<Content>: NSViewControllerRepresentable where Content: View {

    let content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    func makeNSViewController(context: NSViewControllerRepresentableContext<ToolbarTabView>) -> NSTabViewController {
        let vc = NSTabViewController()
        vc.tabStyle = .toolbar


        for item in self.content() as! something? {
            let t = NSTabViewItem(viewController: NSHostingController(rootView: item))
            t.image = item.image
            t.label = item.label
            t.identifier = item.identifier
            vc.addTabViewItem(t)
        }

        return vc
    }

    func updateNSViewController(_ nsViewController: NSTabViewController, context: NSViewControllerRepresentableContext<ToolbarTabView>) {
    }

    typealias NSViewControllerType = NSTabViewController


}

SwiftUI是否可以实现这种功能? TabView内容如何转换并用于获取图像和标签信息?

1 个答案:

答案 0 :(得分:0)

您不仅可以将内容传递到您的ToolbarTabView,还可以采用包含其他值的数组。尝试这样的事情:

struct ToolbarTabView<Content>: NSViewControllerRepresentable where Content: View {

    let tabs: [(imageName: String, label: String, identifier: Int, content: () -> Content)]

    init(tabs: [(imageName: String, label: String, identifier: Int, content: () -> Content)]) {
        self.tabs = tabs
    }

    func makeNSViewController(context: NSViewControllerRepresentableContext<ToolbarTabView>) -> NSTabViewController {
        let vc = NSTabViewController()
        vc.tabStyle = .toolbar


        for item in tabs {
            let t = NSTabViewItem(viewController: NSHostingController(rootView: item.content()))
            //t.image = item.image
            t.label = item.label
            t.identifier = item.identifier  // this causes an "unrecognized selector" error, but maybe I'm passing a bad value for this, I'm not sure what it expects
            vc.addTabViewItem(t)
        }

        return vc
    }

    func updateNSViewController(_ nsViewController: NSTabViewController, context: NSViewControllerRepresentableContext<ToolbarTabView>) {
    }

    typealias NSViewControllerType = NSTabViewController


}

struct ToolbarTabView_Previews: PreviewProvider {
    static var previews: some View {
        ToolbarTabView(tabs: [
            (
                imageName: "My Image",
                label: "tab1",
                identifier: 0,
                content: {
                    Text("testing")
                }
            )
        ])
    }
}