Maksym Rohovskoi
Maksym Rohovskoi

Reputation: 103

How can I hide TabBar Swift UI?

I have a TabBarView in the root view. In one of the parent views that's nested within the root view, I'd like the tab bar to hide when navigating from that parent view to the child view. Is there any func or command to handle that?

Something like this:

ContentView (with TabBarView) - > ExploreView (Called in TabBarView ) -> MessagesView (Child of ExploreVIew - Hide Tab bar)

My code can be seen below.

TabView{

    NavigationView{
        GeometryReader { geometry in
            ExploreView()
                .navigationBarTitle(Text(""), displayMode: .inline)
                .navigationBarItems(leading: Button(action: {
                }, label: {
                    HStack{
                        Image("cityOption")
                        Text("BER")
                        Image("arrowCities")
                    }.foregroundColor(Color("blackAndWhite"))
                        .font(.system(size: 12, weight: .semibold))
                }),trailing:
                    HStack{
                        Image("closttop")
                            .renderingMode(.template)
                            .padding(.trailing, 125)

                        NavigationLink(destination: MessagesView()
                            .navigationBarTitle(Text("Messages"), displayMode: .inline)
                            .navigationBarItems(trailing: Image("writemessage"))
                            .foregroundColor(Color("blackAndWhite"))
                        ){
                            Image("messages")
                        }
                    }.foregroundColor(Color("blackAndWhite"))
            )
        }
    }
    .tabItem{
        HStack{
            Image("clostNav").renderingMode(.template)
            Text("Explore")
                .font(.system(size: 16, weight: .semibold))
        }.foregroundColor(Color("blackAndWhite"))
    }
    SearchView().tabItem{
        Image("search").renderingMode(.template)
        Text("Search")
    }
    PostView().tabItem{
        HStack{
            Image("post").renderingMode(.template)
                .resizable().frame(width: 35, height: 35)
        }
    }
    OrdersView().tabItem{
        Image("orders").renderingMode(.template)
        Text("Orders")
    }
    ProfileView().tabItem{
        Image("profile").renderingMode(.template)
        Text("Profile")
    }
}

Appreciate any help! Thanks!

Upvotes: 1

Views: 4465

Answers (3)

Michael Wang
Michael Wang

Reputation: 10223

A bit late here, on iOS 16 you could use ContentView().toolbar( .hidden, for: .tabBar)

So in your case, it would be like as below,

struct ExploreView: View {
    var body: some View {
        some_view {

        }
        .toolbar(.hidden, for: .tabBar)
    }
}

Upvotes: 1

Chaman Sharma
Chaman Sharma

Reputation: 636

Create CustumPresentViewController.swift -

    import UIKit
    import SwiftUI

    struct ViewControllerHolder {
         weak var value: UIViewController?
    }

    struct ViewControllerKey: EnvironmentKey {
    static var defaultValue: ViewControllerHolder { return 
        ViewControllerHolder(value: 
        UIApplication.shared.windows.first?.rootViewController ) }
    }

    extension EnvironmentValues {
        var viewController: ViewControllerHolder {
            get { return self[ViewControllerKey.self] }
            set { self[ViewControllerKey.self] = newValue }
      }
    }

    extension UIViewController {
         func present<Content: View>(style: UIModalPresentationStyle = 
         .automatic, @ViewBuilder builder: () -> Content) {
             // Must instantiate HostingController with some sort of view...
             let toPresent = UIHostingController(rootView: 
              AnyView(EmptyView()))
             toPresent.modalPresentationStyle = style
             // ... but then we can reset rootView to include the environment
             toPresent.rootView = AnyView(
             builder()
            .environment(\.viewController, ViewControllerHolder(value: 
             toPresent))
    )
    self.present(toPresent, animated: true, completion: nil)
    }
}

Use this in required View -

@Environment(\.viewController) private var viewControllerHolder:  
ViewControllerHolder

private var viewController: UIViewController? {
    self.viewControllerHolder.value
}

var body: some View {
    NavigationView {
        ZStack {
            Text("Navigate")
        }.onTapGesture {
             self.viewController?.present(style: .fullScreen) {
                                    EditUserView()
             }
        }
    }
}

Upvotes: 2

Chris
Chris

Reputation: 8126

in a normal iphone environment the tabbar vanishes from itself if you are navigating....or are u running in another environment?

check this:

struct ContentView: View {

    @State var hideTabbar = false
    @State var navigate = false

    var body: some View {
        NavigationView {

            TabView {

                VStack {
                    NavigationLink(destination: Text("without tab")){
                      Text("aha")
                    }
                        .tabItem {
                            Text("b")
                    }
                }.tag(0)
                Text("Second View")
                    .tabItem {
                        GeometryReader { geometry in

                            Image(systemName: "2.circle")
                            //        Text("Second")
                            Text("\(geometry.size.height) - \(geometry.size.width)")

                        }
                }.tag(1)
            }
        }
    }//.isHidden(self.hideTabbar)
}

Upvotes: 0

Related Questions