xcode 如何隐藏TabBar Swift用户界面?

xqkwcwgp  于 2023-02-20  发布在  Swift
关注(0)|答案(3)|浏览(191)

我在根视图中有一个TabBarView。在嵌套在根视图中的一个父视图中,我希望在从该父视图导航到子视图时隐藏标签栏。有什么函数或命令可以处理这个问题吗?
大概是这样的
ContentView (with TabBarView) - > ExploreView (Called in TabBarView ) -> MessagesView (Child of ExploreVIew - Hide Tab bar)
我的代码可以在下面看到。

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

感谢任何帮助!谢谢!

7ivaypg9

7ivaypg91#

创建自定义显示视图控制器。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)
    }
}

在所需视图中使用此-

@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()
             }
        }
    }
}
cx6n0qe3

cx6n0qe32#

在iOS 16上,您可以使用ContentView().toolbar(.hidden,用于:.标签栏)
所以在你的例子中,它会像下面这样,

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

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

jogvjijk3#

在正常的iPhone环境中,如果你在导航,标签栏会自动消失...或者你在另一个环境中运行?
检查以下内容:

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

相关问题