Building a custom TabView-like view in SwiftUI

Viewed 1367

I've been trying to build a toolbar-tabview component for macOS that can be composed with child views just like the TabView like bellow:

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)
        }
    }
} 

At the moment I've something like this:

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


}

Is something like this possible with SwiftUI? How is TabView content being casted and used to get Image and Label information?

1 Answers

Instead of just passing content to your ToolbarTabView, you can have it take an array that also contains other values. Try something like this:

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")
                }
            )
        ])
    }
}
Related