SwiftUI hide TabBar in subview SwiftUI hide TabBar in subview xcode xcode

SwiftUI hide TabBar in subview


iOS 14

Install the Introspect SwiftPM: https://github.com/siteline/SwiftUI-Introspect

struct SomeView: View{        @State var uiTabarController: UITabBarController?        var body: some View {        List {            -your code here-        }                .navigationBarTitle("Title", displayMode: .inline)        .introspectTabBarController { (UITabBarController) in            UITabBarController.tabBar.isHidden = true            uiTabarController = UITabBarController        }.onDisappear{            uiTabarController?.tabBar.isHidden = false        }    }}

In this code in uiTabarController, we are taking the reference of UITabarController. When we go back then we enabled the Tabar again. So, that's why this is needed.


here's no way to hide TabView so I had to add TabView inside ZStack as this:

var body: some View {    ZStack {        TabView {            TabBar1().environmentObject(self.userData)                .tabItem {                    Image(systemName: "1.square.fill")                    Text("First")            }            TabBar2()                .tabItem {                    Image(systemName: "2.square.fill")                    Text("Second")            }        }        if self.userData.showFullScreen {            FullScreen().environmentObject(self.userData)        }    }}

UserData:

  final class UserData: ObservableObject {    @Published var showFullScreen = false}

TabBar1:

struct TabBar1: View {    @EnvironmentObject var userData: UserData    var body: some View {        Text("TabBar 1")            .edgesIgnoringSafeArea(.all)            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)            .background(Color.green)            .onTapGesture {                self.userData.showFullScreen.toggle()        }    }}

FullScreen:

struct FullScreen: View {    @EnvironmentObject var userData: UserData    var body: some View {        Text("FullScreen")            .edgesIgnoringSafeArea(.all)            .frame(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)            .background(Color.red)            .onTapGesture {                self.userData.showFullScreen.toggle()        }    }}

check full code on Github

there's also some other ways but it depends on the structure of the views


iOS 14 Simple Solution

Install the Introspect SwiftPM: https://github.com/siteline/SwiftUI-Introspect

var body: some View {    List {        -your code here-    }        .navigationBarTitle("Title", displayMode: .inline)    .introspectTabBarController { (UITabBarController) in        UITabBarController.tabBar.isHidden = true    }}

NOTE: You have to re-enable the TabBar it in the parent view or it will still be hidden.

.introspectTabBarController { (UITabBarController) in            UITabBarController.tabBar.isHidden = false}