0

I'm trying to find a way for the tabBar to become hidden upon the appearance of SecondView(). Every attempted solution I've seen so far hasn't worked (or I've implemented it incorrectly). Is there a straightforward way to implement this?

Code:

struct MainView: View {
    var body: some View {
        TabView {
            FirstView()
                .tabItem {
                    Image(systemName: "house.fill")
                }
        }
    }
}

struct FirstView: View {
    var body: some View {
        NavigationView {
            NavigationLink(
                destination: SecondView(),
                label: {
                    Text("Second")
                }
            )
        }
    }
}

struct SecondView: View {
    var body: some View {
        Text("Second")
    }
}
Sutton
  • 53
  • 3
  • 2
    Does this answer your question? [SwiftUI hide TabBar in subview](https://stackoverflow.com/questions/58444689/swiftui-hide-tabbar-in-subview) – koen Mar 29 '23 at 10:47
  • I have to mention that a Tab Bar in an app should be persistent throughout any navigation that uses a NavigationView or NavigationStack. Hiding it like this is not recommended from Apple. If you want to hide it for a specific feature like this you might want to look at using something like a `.sheet` to present a view over it. – Fogmeister Mar 29 '23 at 11:52

1 Answers1

0
struct MainView: View {
  var body: some View {
    TabView {
      FirstView()
        .tabItem {
          Image(systemName: "house.fill")
        }
    }
  }
}

struct FirstView: View {
  var body: some View {
    NavigationStack { // << HERE!
      NavigationLink(
        destination: SecondView(),
        label: {
          Text("Second")
        }
      )
    }
  }
}

struct SecondView: View {
  var body: some View {
    Text("Second")
      .toolbar(.hidden, for: .tabBar) // << HERE!
  }
}

// `NavigationStack` and `toolbar(_:for:)` are available for iOS 16.0/macOS 13.0
zzzwco
  • 184
  • 6