0

The TabView works well most of the time. But when the App launch the first time, the selection not working.

Here is the code,

struct ContentView: View {
    
    @State private var selection: Int = KV.lastTabPosition
    @State private var showFab: Bool = true
    
    var body: some View {
        NavigationView {
            ZStack {
                Color(R.color.background_color()!).ignoresSafeArea(.all)
                VStack(spacing: 0) {
                    HomeToolbar(selection: $selection)
                    ZStack {
                        TabView(selection: $selection) {
                            passwordsView.tag(0)
                            favoriatedView.tag(1)
                            settingsView.tag(2)
                        }.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
                        HomeFabContainer(selection: $selection, showFab: $showFab)
                            .padding(EdgeInsets(top: 0, leading: 0, bottom: 15, trailing: 15))
                    }
                    Color(R.color.divider_color()!)
                        .frame(height: 1)
                    HomeNavigationBar(selection: $selection)
                }
            }
        }
        .onAppear(perform: {
            L.d { "reloading data from content view." }
            PasswordRepo.shared.load()
            self.selection = KV.lastTabPosition
        })
        .onDisappear {
            KV.lastTabPosition = selection
        }
    }
    
    private var passwordsView: some View {
        PasswordsView(showFab: $showFab)
    }
    
    private var favoriatedView: some View {
        FavoriatedView()
    }
    
    private var settingsView: some View {
        SettingsView()
    }
}

struct HomeNavigationBar: View {
    
    private let tabs: [String] = HomeTab.names
    
    private let icons: [String] = HomeTab.icons

    private let iconsFilled: [String] = HomeTab.iconsFilled

    @Binding
    var selection: Int
    
    var body: some View {
        HStack {
            ForEach(0..<3) { index in
                HStack {
                    Spacer()
                    VStack(spacing: 10, content: {
                        Image(systemName: selection == index ? iconsFilled[index] : icons[index])
                            .foregroundColor(selection == index ? Color(R.color.accent()!) : Color(R.color.text_color_secondary()!))
                        Text(tabs[index])
                            .font(.system(size: 14))
                            .foregroundColor(selection == index ? Color(R.color.accent()!) : Color(R.color.text_color_secondary()!))
                    })
                    Spacer()
                }.contentShape(Rectangle()).onTapGesture {
                    self.selection = index
                }
            }
        }.frame(width: UIScreen.main.bounds.width)
            .padding(EdgeInsets(top: 6, leading: 0, bottom: 6, trailing: 0))
    }
}

As the code above, I used a TabView and a customed Navigation view here. And I used the KV to remember the last selection of tab. And when the next time launch the App, I will make the homepage show the last selected tab.

The problem is, when I left the App the selected tab is the 3rd one, and when I re-launch the App, the default selected tab is also the 3rd one. But, when I clicked the first one tab by my custom navigation bar, the navigtaion bar and the title bar state are changed accordingly, but the displayed tab content is still the 3rd one. When I click other tab and then click the first tab again (by navigation bar), it displayed as expected.

I doubt this is a SwiftUI bug.

Shawn Wong
  • 554
  • 6
  • 15

0 Answers0