swiftui rootviewcontroller 隐藏状态栏
时间: 2023-09-10 22:03:39 浏览: 290
在SwiftUI中隐藏状态栏,在rootViewController中可以采用以下方法:
首先,我们需要创建一个自定义的UIHostingController子类,并重写它的preferredStatusBarStyle属性,以控制状态栏的样式。这个类将作为rootViewController。
```swift
class CustomHostingController<Content: View>: UIHostingController<Content> {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent // 这里可以根据需要设定状态栏的样式
}
}
```
然后,在SceneDelegate中,我们将创建一个CustomHostingController,并将其设置为window的rootViewController。
```swift
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// 保留原有的代码
// 使用自定义的HostingController作为rootViewController
let contentView = ContentView() // 这里替换为你自己的内容视图
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = CustomHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
// 保留原有的方法
}
```
这样,我们就成功地将自定义的CustomHostingController作为rootViewController,同时也隐藏了状态栏。
阅读全文
相关推荐
















