.toolbar(.hidden, for: .tabBar) not working under iOS 17.4

217 views Asked by At

I'm experiencing an issue with hiding the tab bar in a SwiftUI TabView after updating both my simulator and physical device to iOS 17.4.

TabView(selection: $tabSelection) {
    Group {
        View1()
            .tag(0)

        View2()
            .tag(1)
    }
    .toolbar(.hidden, for: .tabBar)
}

View1 is the default view and contains a scroll view. Initially, upon launching the app, everything works as expected, and the tab bar is correctly hidden. The problem arises when I switch to View2 (where the tab bar remains hidden as it should), but then upon returning to View1, the tab bar becomes visible again.

Does anyone have any ideas how to fix this behaviour?

I checked on previous iOS versions and it seems this only occurs on iOS 17.4.

2

There are 2 answers

0
cbear84 On

I am not sure what behavior you are looking for but you might want to put the modifier on the view. Not sure if this helps. You can place the NavigationStack and NavigationLink on this view to help navigation.

struct ContentView: View {
    var body: some View {
        TabView {
            Text("Home")
                .tabItem {
                    Label("Home", systemImage: "house")
                }
            
            Text("Settings")
                .tabItem {
                    Label("Settings", systemImage: "gear")
                }
            View1()
                .tabItem {
                    Label("More", systemImage: "1.circle")
                }
            View2()
                .tabItem {
                    Label("More", systemImage: "2.circle")
                }
                .toolbar(.hidden, for: .tabBar)
        }
    }
}

#Preview {
    ContentView()
}


struct View1: View {
    var body: some View {
        ScrollView {
            Text("View 1")
        }
    }
}
struct View2: View {
    var body: some View {
        ScrollView {
            Text("View 2")
        }
    }
}
0
Russell Neufeld On

I hit the same problem. Seems like Apple busted a bunch of things in iOS 17.4. I was able to work around this by explicitly setting the visibility of the tab bar in various .onAppear/.onDisappear closures. In particular, I defined

@State var visibility = Visibility.visible

and

.toolbar(visibility, for: .tabBar)

in one of my main views and then passed visibility to a @Binding in a bunch of other views, and then set

visibility = .visible

or

visibility = .hidden

in various places.

Seems to work (thankfully) on previous versions of iOS 17 and 16.