Dynamically hiding tabs in SwiftUI TabView - Tabs mixed up in More... overflow item

Viewed 93

When you use an if statement in the SwiftUI TabView to dynamically hide/show tabs, SwiftUI will end up mixing up content in the 'More...' overflow tab item.

TabView() {
    if someCondition {
        View1()
    }
    View2()
    // ... more views
}

Steps to reproduce: Check out https://github.com/ralfebert/SwiftUITabItemOverflowBug , use the 'Show Moon' toggle which hides a tab. Go to the 'More...' overflow tab, and select 'trash'. This will now show the incorrect view - 'smoke' instead of the 'trash' one:

Seems like a SwiftUI bug (FB9078171). Is there a workaround to get the correct behaviour when dynamically showing and hiding tabs?

incorrect tab shown

1 Answers

Try this one

TabView() {
    if someCondition {
        View1()
    }
    View2()
    // ... more views
}
.id(someCondition)          // << here !!
Related