Thursday, June 13, 2024
HomeiOS Developmentios - Find out how to forestall views from destroying in my...

ios – Find out how to forestall views from destroying in my customized tab bar in SwiftUI?


I’ve my very own TabBar as a result of native SwiftUI’s TabBar has many restrictions. Here’s a code:

enum Tab: CaseIterable {
    case myWords
    case addWord
    case coaching
    case settings
}

@Themed struct TabBarView: View {
    @State non-public var selectedTab: Tab = .myWords
    
    var physique: some View {
        ZStack(alignment: .backside) {
            VStack(spacing: 0) {
                change selectedTab {
                case .myWords: makeNavigationView(for: .myWords) { MyWordsView() }
                case .addWord: makeNavigationView(for: .addWord) { AddWordView() }
                case .coaching: makeNavigationView(for: .coaching) { QuickTrainingView() }
                case .settings: makeNavigationView(for: .settings) { SettingsView() }
                }
                
                HStack(spacing: 0) {
                    TabButton(textual content: "My phrases", picture: "Tabs/Phrases", chosen: makeBinding(for: .myWords))
                    TabButton(textual content: "Add phrase", picture: "Tabs/Add", chosen: makeBinding(for: .addWord))
                    TabButton(textual content: "Coaching", picture: "Tabs/Play", chosen: makeBinding(for: .coaching))
                    TabButton(textual content: "Settings", picture: "Tabs/Settings", chosen: makeBinding(for: .settings))
                }
            }
            .ignoresSafeArea(.keyboard)
        }
    }
    
    non-public func makeNavigationView(for tab: Tab, content material: () -> some View) -> some View {
        NavigationView {
            content material()
                .navigationBarTitleDisplayMode(.inline)
                .navigationTitle(String(localeKey: tab.title))
        }
        .navigationViewStyle(StackNavigationViewStyle())
        .accentColor(theme.shade(.lightBlue))
    }
    
    non-public func makeBinding(for tab: Tab) -> Binding <Bool> {
        Binding <Bool>(get: { selectedTab == tab },
                       set: { if $0 == true { selectedTab = tab } })
    }
}

The issue is after I change to a different view (change selectedTab to a different worth), the earlier view’s state is misplaced. So after I return my tab view is recreated and misplaced its state. Particularly it is annoying after I navigate in NavigationView within my tab. So I can not perceive how you can “cache” my tab views, I imply save their state or forestall to recreating these views. Perhaps I simply ought to to cover views, however I do not perceive how you can do it



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments