我正在努力隐藏navigationBar,如果根控制器不是SwiftUI UIHostingController,它将正确隐藏。
我尝试了以下方法:

  • 在创建navigationController.isNavigationBarHidden = true后将其设置为viewDidLoadviewWillAppear
  • .navigationBarHidden(true).navigationBarBackButtonHidden(true)添加UIHostingControllerrootView

  • 可能是Apple的错误吗?我正在使用Xcode 11.6。
    我所有的尝试在一起:
    class LoginController: UINavigationController, ObservableObject
    {
        static var newAccount: LoginController
        {
            let controller = LoginController()
            let view = LoginViewStep1()
                .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
            controller.viewControllers = [UIHostingController(rootView: view)]
            controller.isNavigationBarHidden = true
            return controller
        }
    
        override func viewWillAppear(_ animated: Bool)
        {
            super.viewWillAppear(animated)
    
            self.isNavigationBarHidden = true
        }
    
        override func viewDidLoad()
        {
            super.viewDidLoad()
    
            self.isNavigationBarHidden = true
        }
    }
    
    struct LoginViewStep1: View
    {
        // ...
    
        var body: some View
        {
            VStack {
                // ...
            }
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
        }
    }
    

    最佳答案

    这是一个解决方案。经过Xcode 11.4 / iOS 13.4测试
    ios - 当根 Controller 是UIHostingController时,隐藏UINavigationController的navigationBar-LMLPHP
    修改您的代码:

    class LoginController: UINavigationController, ObservableObject
    {
        static var newAccount: LoginController
        {
            let controller = LoginController()
            let view = LoginViewStep1()
            controller.viewControllers = [UIHostingController(rootView: view)]
    
            // make it delayed, so view hierarchy become constructed !!!
            DispatchQueue.main.async {
                controller.isNavigationBarHidden = true
            }
    
            return controller
        }
    }
    
    struct LoginViewStep1: View
    {
        var body: some View
        {
            VStack {
                Text("Hello World!")
            }
        }
    }
    
    SceneDelegate中经过测试的部分
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)
    
        window.rootViewController = LoginController.newAccount
    
        self.window = window
        window.makeKeyAndVisible()
    }
    

    关于ios - 当根 Controller 是UIHostingController时,隐藏UINavigationController的navigationBar,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63367333/

    10-12 04:11
    查看更多