navigationBarHidden() Not working in iOS 15 Beta SwiftUI

Viewed 492

I am trying to hide the Navigation bar on the 3rd tab but for the rest tabs, it should show up. It workes fine in iOS 14, but in iOS 15 the modifier randomly workes. As soon as I start switching between tabs the navbar starts showing up on the 3rd tab as well. Below is my SwiftUI file Code

NOTE: This is just a simple representation of a more complex code setup I have for my app. Xcode: 12.5.1 Device: XR (iOS 15 beta)

import SwiftUI

struct TestView: View {
    
    @State var selectedTab: TabType = .home
    
    var body: some View {
        NavigationView {
            TabView(selection: $selectedTab) {
                ForEach(TabType.allCases, id: \.self) { tabType in
                    switch tabType {
                    case .profile:
                        ThirdView()
                            .navigationBarHidden(true)
                            .tag(tabType)
                            .tabItem {
                                Label(tabType.title, systemImage: "plus")
                            }
                    default:
                        SecondView()
                            .tag(tabType)
                            .tabItem {
                                Label(tabType.title, systemImage: "star")
                            }
                    }
                }
            }
        }
    }
}

struct SecondView: View {
    
    var body: some View {
        VStack {
            Spacer()
            Text("2nd view")
            NavigationLink("3rd view", destination: ThirdView())
            Spacer()
        }
        .background(Color.green)
    }
}

struct ThirdView: View {
    
    var body: some View {
        ZStack {
            Color.red
            Text("3rd view")
        }
    }
}

enum TabType: CaseIterable {
    
    case home
    case discover
    case profile
    case more
    
    var image: Image {
        switch self {
        case .home:
            return Image(systemName: "plus")
        case .discover:
            return Image(systemName: "plus")
        case .profile:
            return Image(systemName: "plus")
        case .more:
            return Image(systemName: "plus")
        }
    }
    
    var title: LocalizedStringKey {
        switch self {
        case .home:
            return LocalizedStringKey("home")
        case .discover:
            return LocalizedStringKey("Discover")
        case .profile:
            return LocalizedStringKey("Me")
        case .more:
            return LocalizedStringKey("Shop")
        }
    }
}
1 Answers

Move your .navigationBarHidden(true) to your ThirdView's ZStack like this:

struct ThirdView: View {
    
    var body: some View {
        ZStack {
            Color.red
            Text("3rd view")
        }.navigationBarHidden(true)
    }
}

Also if you do this you will lose your back button...

Make sure to remove it from the TabView first.

Lmk if it works!

Related