[英]How to change status bar color?
在我的應用程序中,我使用圖像作為ViewController's
. 對於我設置的項目設置中的Status Bar Style - Default
: Status Bar Style - Default
。 我不使用其他任何東西作為狀態欄。
問題是當啟用 iOS 暗模式時,我的status bar
變為白色。 我需要它保持黑色。 如何解決?
此外,我不想關閉應用程序中支持的 iOS 暗/亮模式。 所以Info.plist
中的Appearance Light對我來說不太適用。
將狀態欄樣式設置為深色內容:
之后添加您的 info.plist查看基於控制器的狀態欄外觀並將其設置為NO
更新
如果您只想要確定控制器中的暗內容,請在 viewWillAppear 中添加 setNeedsStatusBarAppearanceUpdate,然后覆蓋 preferredStatusBarStyle:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}
從導航控制器開始:
在您的場景委托中聲明您的第一個導航控制器:
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()
let controller = UINavigationController(rootViewController: FirstViewController())
controller.navigationBar.barStyle = .black
window?.rootViewController = controller
}
在 SecondViewController 中覆蓋狀態欄樣式
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13.0, *) {
return .darkContent
} else {
return .default
}
}
對於每個ViewController
您可以使用簡單的覆蓋方法設置狀態欄顏色。
override var preferredStatusBarStyle: UIStatusBarStyle {
if #available(iOS 13, *) {
return .darkContent
} else {
return .default
}
}
不要忘記在 Info.plist 中將基於視圖控制器的狀態欄外觀設置為YES 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.