简体   繁体   中英

TabView does not work correctly on iOS13 SwiftUI

I created a TabView with 4 items but with iOS 13.x only the first view is displayed correctly. When I click on another item's icon, the view is not shown correctly but the app only shows a white view. If I run the app on iOS > 14 I can correctly view all the views.

TabView implementation:

struct ContentView: View {

private enum Tab: Hashable {
       case discovery
       case qrcode
       case devices
       case settings
}
   
@State private var selectedTab: Tab = .discovery

var body: some View {
    NavigationView {
    TabView(selection: $selectedTab) {
        DiscoveryView()
            .tabItem {
                VStack {
                    Image(systemName: "lock.rotation.open")
                    Text("Discovery")
                }
            }
            .tag(0)
        QrCodeView()
            .tabItem {
                VStack {
                    Image(systemName: "qrcode.viewfinder")
                    Text("QrCode")
                }
            }
            .tag(1)
        DevicesView()
            .tabItem {
                VStack {
                    Image(systemName: "qrcode.viewfinder")
                    Text("My devices")
                }
            }
            .tag(2)
        SettingsView()
            .tabItem {
                VStack {
                    Image(systemName: "gear")
                    Text("Settings")
                }
            }
            .tag(3)
        }
    }
}

Implementation of one of the views:

struct QrCodeView: View {
    var body: some View { 
        Text("QrCode")
    }
}

Where am I doing wrong?

It might be a reason of selection ... selection and tag types should be the same, so try

@State private var selectedTab: Tab = .discovery

var body: some View {
    NavigationView {
    TabView(selection: $selectedTab) {
        DiscoveryView()
            .tabItem {
                VStack {
                    Image(systemName: "lock.rotation.open")
                    Text("Discovery")
                }
            }
            .tag(.discovery)      // << here !!

        QrCodeView()
            .tabItem {
                VStack {
                    Image(systemName: "qrcode.viewfinder")
                    Text("QrCode")
                }
            }
            .tag(.qrcode)        // << here !!

 // ... others the same

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