swiftswiftuiswiftui-tabviewvisionos

Is there a bug in visionOS TabBar Preview?


Playing around with visionOS I'm rewriting SwiftUI tutorials to launch on Xcode visionOS simulator, everything's working fine so far, but adding any TabView makes preview crash (even when it's working on running). Here's my code.

TabView(selection: $selection) {
    Text("Tab 1")
        .tabItem {
            Label("Featured", systemImage: "star")
        }
        .tag(Tab.featured)

    Text("Tab 2")
        .tabItem {
            Label("List", systemImage: "list.bullet")
        }
        .tag(Tab.list)
 }

Anything I might be missing?


Solution

  • There is no bug in visionOS. Put TabView inside NavigationSplitView.

    import SwiftUI
    
    struct ContentView : View {
        
        @State var text: String = ""
        
        var body: some View {
            NavigationSplitView {
                TabView {
                    Text("Tab One")
                        .tabItem { Label("Featured", systemImage: "star") }
                        .onAppear { text = "Tab One's Items" }
                    Text("Tab Two")
                        .tabItem { Label("List", systemImage: "list.bullet") }
                        .onAppear { text = "Items of Tab Two" }
                }.tabViewStyle(.page)
                List {
                    Text(text)
                        .onChange(of: text) { _, newValue in
                            text = newValue
                        }
                }
            } detail: {
                // some code
            }
        }
    }
    #Preview {
        ContentView()
    }