gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 05:00:28 34 4
gpt4 key购买 nike

我有一个 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/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com