Riv*_*era 7 uinavigationbar uinavigationcontroller ios swiftui uihostingcontroller
我正在努力隐藏navigationBar
,如果根控制器不是 SwiftUI,它将正确隐藏UIHostingController
。
我尝试了以下方法:
navigationController.isNavigationBarHidden = true
创建后设置,在viewDidLoad
和viewWillAppear
。
为's添加.navigationBarHidden(true)
和。.navigationBarBackButtonHidden(true)
UIHostingController
rootView
这可能是苹果的一个错误吗?我使用的是 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)
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个解决方案。使用 Xcode 11.4 / iOS 13.4 进行测试
修改了你的代码:
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!")
}
}
}
Run Code Online (Sandbox Code Playgroud)
测试部分在SceneDelegate
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = LoginController.newAccount
self.window = window
window.makeKeyAndVisible()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2953 次 |
最近记录: |