I want to change status bar style based on the screen content. For darker screens, status bar content should be white. For lighter screens, status bar content should be black.
It seems that the problem only occurs in iOS 15 devices.
Below screenshot compares the same use case on both iOS 15.2 & iOS 14.5
As you can see, status bar style is defaulted even though navigation bar style is set to "black"
class ViewAController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// setup views ...
}
// setting navigation bar style doesn't work iOS 15. Needed to set UINavigationAppearance()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.barTintColor = .white
self.navigationController?.navigationBar.barStyle = .default
self.navigationController?.navigationBar.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.black]
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .white
appearance.shadowImage = UIImage()
appearance.shadowColor = .clear
appearance.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.black]
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
}
}
@objc private func onClick(_ sender: UIButton) {
let vc = ViewBViewController()
self.show(vc, sender: self)
}
}
class ViewBViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
self.navigationItem.title = "View B"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.isTranslucent = false
self.navigationController?.navigationBar.barTintColor = .black
self.navigationController?.navigationBar.barStyle = .black
self.navigationController?.navigationBar.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.white]
// setting navigation bar style doesn't work iOS 15. Needed to set UINavigationAppearance()
if #available(iOS 15, *) {
let appearance = UINavigationBarAppearance()
appearance.configureWithOpaqueBackground()
appearance.backgroundColor = .black
appearance.shadowImage = UIImage()
appearance.shadowColor = .clear
appearance.titleTextAttributes = [.font: UIFont.boldSystemFont(ofSize: 18), .foregroundColor: UIColor.white]
self.navigationItem.standardAppearance = appearance
self.navigationItem.scrollEdgeAppearance = appearance
}
}
}