I want to hide TabView bottom TabBar if user navigate from next screen.
For example I have TabView that have two tabItem let’s say Home and Account and home screen have notification option if user click notification I want to navigate to notification screen at the same time I want to hide TabView bottom Tab also. Is Any way to achieve this. I can’t find any direct SwiftUI way to achieve this.
import SwiftUI
struct MyRootScreen: View {
var body: some View {
TabView {
NavigationView {
HomeScreen()
}
.tabItem {
Image(systemName: "house")
}
Text("Account")
.tabItem {
Image(systemName: "person")
}
}
}
}
struct HomeScreen: View {
@State private var showNotification: Bool = false
var body: some View {
VStack {
Button {
showNotification = true
} label: {
Text("Show Notification")
}
}
.overlay {
NavigationLink("", isActive: $showNotification) {
Text("Notification Screen")
}
}
}
}
Note: I want to to achieve this without wrapping TabView into NavigationView.
2
Answers
It would be better to move the notification screen to your root view and pass the boolean flag as binding that will control notifications.
Or even better: since your child views will need to only open notifications, and never hide, you can pass a function instead of a binding. It can also be passed in the environment, but be careful with retain cycles.
Here is an example. If you don’t need Notifications to take the full screen, replace
fullScreenCover
withsheet
:So all of the possible ways have been shared above. But i am just concluding them in a single answer and also adding my suggestion too.
. toolbar
API to hide the tabviewHere are the more details for all of three approaches.
Option 1st is the best way to preset a modal or sheet and show your notification view over there, But it totally depend upon your navigations style or other requirements in your app.
Using option 2nd which is recommended one, you can go ahead but if you are only on iOS 14.0 with 16.0 you can try the apis provided by apple itself.
Using option 3rd its possible but you have to manage everyting by your own on that view, like giving a back button, nav title and its actions etc,
If you want to see some sample code here is the below for 3rd approach.
Attaching a working video of it.
I hope it helps
It’s working for me on Xcode 14.3.