admin管理员组文章数量:1287085
This question Swift: Setting StatusBar color on IOS13+ (Using statusBarManager) has a very good answer on how to set the statusbar text color to dark or light for the whole app or for a UIViewController.
But there is no answer on how to change the statusbar appearance dynamically.
This still works for me, but it is already deprecated since iOS 13:
// inside a function of the viewController
//set the status bar background
overrideUserInterfaceStyle = darkMode ? .dark : .light
// on older iOS, this is not sufficient (tested and failed on ios 15)
setNeedsStatusBarAppearanceUpdate()
// statusBarStyle is deprecated on iOS 13+
UIApplication.shared.statusBarStyle = nightMode ? .lightContent : .darkMode
This does not work for me (the statusbar style is set when loading the view, but not updated later):
override var preferredStatusBarStyle: UIStatusBarStyle {
return darkMode ? .lightContent : .darkContent
}
This question Swift: Setting StatusBar color on IOS13+ (Using statusBarManager) has a very good answer on how to set the statusbar text color to dark or light for the whole app or for a UIViewController.
But there is no answer on how to change the statusbar appearance dynamically.
This still works for me, but it is already deprecated since iOS 13:
// inside a function of the viewController
//set the status bar background
overrideUserInterfaceStyle = darkMode ? .dark : .light
// on older iOS, this is not sufficient (tested and failed on ios 15)
setNeedsStatusBarAppearanceUpdate()
// statusBarStyle is deprecated on iOS 13+
UIApplication.shared.statusBarStyle = nightMode ? .lightContent : .darkMode
This does not work for me (the statusbar style is set when loading the view, but not updated later):
override var preferredStatusBarStyle: UIStatusBarStyle {
return darkMode ? .lightContent : .darkContent
}
Share
Improve this question
asked Feb 25 at 7:58
Lorenz MeyerLorenz Meyer
19.9k23 gold badges82 silver badges128 bronze badges
1 Answer
Reset to default 2Make sure info.plist
doesn't contains Appearance
Than implement code like given below in controller
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/
override var preferredStatusBarStyle: UIStatusBarStyle {
return UIScreen.main.traitCollection.userInterfaceStyle == .dark ? .lightContent : .darkContent
}
}
i tried this in iOS16.0+
Check with it and let me know if you have any issue in this:)
本文标签: swiftSetting StatusBar dynamically using statusBarManagerStack Overflow
版权声明:本文标题:swift - Setting StatusBar dynamically using statusBarManager - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741221078a2360982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论