SwiftUI隐藏UIKit UINavigationController的导航栏(rootViewController : _)

标签 swift swiftui uikit

我有一个 SwiftUI View :

struct Content View: View {
  var body: some View {
    Text("Content view")
  }
}

我使用了用 UIKit 创建的菜单。我的 View 应该是 UIViewControllerUINavigationController。为了显示我的 swiftUI View ,我在 UINavigationController 中设置了我的 View :

let navigationController = UIHostingController(rootView: Content()).inNavigation()

// inNavigation()
extension UIViewController{
    func inNavigation() -> UIViewController {
        let vc = self
        let navigationController = UINavigationController(rootViewController: self)
        vc.navigationController?.navigationBar.isHidden = true
        vc.navigationController?.setNavigationBarHidden(true, animated: true)
        vc.navigationController?.isNavigationBarHidden = true
        vc.navigationController?.isToolbarHidden = true
        vc.navigationController?.setToolbarHidden(true, animated: true)
        vc.navigationController?.accessibilityFrame = .zero
        
        navigationController.navigationController?.navigationBar.isHidden = true
        navigationController.navigationController?.setNavigationBarHidden(true, animated: true)
        navigationController.navigationController?.isNavigationBarHidden = true
        navigationController.navigationController?.isToolbarHidden = true
        navigationController.navigationController?.setToolbarHidden(true, animated: true)
        navigationController.accessibilityFrame = .zero
        return navigationController
    }
}

我试图隐藏导航栏。但它仍然显示:

enter image description here

最佳答案

UINavigationController 应该在 View 层次结构中才能生效。

这里是工作变体,假设您将它注入(inject) SceneDelegate 中的窗口(使用 Xcode 11.4/iOS 13.4 测试)

extension UIViewController{
    func inNavigation() -> UIViewController {
        let navigationController = UINavigationController(rootViewController: self)
        DispatchQueue.main.async {
            navigationController.isNavigationBarHidden = true
        }
        return navigationController
    }
}

关于SwiftUI隐藏UIKit UINavigationController的导航栏(rootViewController : _),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63098882/

相关文章:

swift - 如何在代码中调用用户事件

ios - Afnetworking multipart/formdata 返回请求在 ios 11 中失败,但在 ios 10 中工作正常

ios - 动画在 Swift 3 Xcode 8.1 中没有动画

ios - UITextView 在 iPhone 5/5s 上切断文本,但在 iPhone 6 上没有

ios - 无法将类型 'Color' 的值分配给类型 'UIColor'

ios - 屏幕顶部边缘的自定义拖动 View 与通知中心冲突

iphone - 如何以编程方式调用 TouchesBegan、TouchesMoved、TouchedEnded

swift - 具有泛型和特化的协议(protocol)

core-data - SwiftUI 核心数据错误 - "Cannot convert value of type ' FetchedResults<GCItem >' to expected argument type ' Range<Int >'"

performance - SwiftUI ObservableObject CPU 峰值