简体   繁体   中英

TabItem switching not working properly in iOS13 devices

I have created demo of Tab bar with two tabItems. Each tab-item have view controller in it.

On switching tabs, it is not showing its respective UI.

This is happening on iOS 13 only.

Code:

struct ContentView: View {
    private enum Tab: Hashable {
           case one
           case two
    }
    @State private var selectedIndex = Tab.one
    var body: some View {
        TabView(selection: $selectedIndex) {
            DetestVXViewController(bgColor: .red).tabItem {
                Text("123")
            }.tag(Tab.one)
            DetestVXViewController(bgColor: .green).tabItem {
                Text("321")
            }.tag(Tab.two)
        }
       
    }
}

====

Attaching images of iOS 13 and 15 both.

在此处输入图像描述

在此处输入图像描述

Using tag seems overkill unless you need to keep track of said tab for quick actions or another purpose. You can still use tag in this though if needed. You can use @State property for the selection also if needed. You can easily switch tabs with the following example.

在此处输入图像描述 在此处输入图像描述

struct ContentView: View {
    var body: some View {
        TabView {
            ForEach(1..<3) { index in
                SomeView(title: "\(index)")
                    .tabItem {
                        Image(systemName: "\(index).circle.fill")
                        Text("\(index)")3
                    }
                //.tag(index)
            }
        }
    }
}

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