简体   繁体   English

如何隐藏 TabBar Swift UI?

[英]How can I hide TabBar Swift UI?

I have a TabBarView in the root view.我在根视图中有一个TabBarView 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!谢谢!

Create CustumPresentViewController.swift -创建 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()
             }
        }
    }
}

in a normal iphone environment the tabbar vanishes from itself if you are navigating....or are u running in another environment?在正常的 iphone 环境中,如果您正在导航,标签栏会自行消失......或者您是否在另一个环境中运行?

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)
}

If you are using Navigation controller then simply call如果您使用的是导航 controller 然后只需调用

self.parent?.navigationController?.pushViewController(yourViewController, animated: true)

Navigating to ChildVC this method will not include your TabBarView导航到ChildVC此方法将不包括您的TabBarView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM