我一直在尝试为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内容如何转换并用于获取图像和标签信息?

最佳答案

您可以让它采用一个还包含其他值的数组,而不仅仅是将内容传递到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")
                }
            )
        ])
    }
}

关于ios - 在SwiftUI中构建自定义的类似于TabView的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58513264/

10-11 01:07