简体   繁体   中英

Views content not showing on switching to other tabs using TabbedView of SwiftUI

I'm implementing TabbedView using SwiftUI framework by referring https://developer.apple.com/documentation/swiftui/tabview

When running on the simulator, only the first tab view contents showing and other tabs contents not showing. Even after restarting XCode, simulator etc.

App video link: https://youtu.be/Gibu8jfQQ5I

struct ContentView : View {
    var body: some View {
         TabbedView {
            Text("The First Tab")
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            Text("Another Tab")
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
            Text("The Last Tab")
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }
        }.font(.headline)
    }
}

Appreciate you help and suggestions!

In beta 5, your code works, and also TabbedView was renamed to TabView . If you cannot upgrade to beta 5 yet, to fix your problem in beta 4, you need to add .tag(n) to each view:

struct ContentView : View {
    var body: some View {
         TabbedView {
            Text("The First Tab").tag(1)
                .tabItem {
                    Image(systemName: "1.square.fill")
                    Text("First")
            }
            Text("Another Tab").tag(2)
                .tabItem {
                    Image(systemName: "2.square.fill")
                    Text("Second")
            }
            Text("The Last Tab").tag(3)
                .tabItem {
                    Image(systemName: "3.square.fill")
                    Text("Third")
            }
        }.font(.headline)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM