1

I have been trying to change Status Bar Color but using key statusBar my app is crashing. I guess key "statusBar" is not available in iOS 13.

let statusBar: UIView = UIApplication.shared.value(forKey: "statusBar") as! UIView
if statusBar.responds(to: #selector(setter: UIView.backgroundColor)) {
  statusBar.backgroundColor = <Some Color>
}

adding view also not working for me.

  • Does this answer your question? [How to change the status bar background color and text color on iOS 13?](https://stackoverflow.com/questions/56651245/how-to-change-the-status-bar-background-color-and-text-color-on-ios-13) – Rushabh Shah Mar 04 '20 at 05:57
  • Share console log please with crash reason. – Dharmesh Kheni Mar 04 '20 at 06:04
  • @DharmeshKheni got this crash: *** Assertion failure in -[UIApplication _createStatusBarWithRequestedStyle:orientation:hidden:], *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'App called -statusBar or -statusBarWindow on UIApplication: this code must be changed as there's no longer a status bar or status bar window. Use the statusBarManager object on the window scene instead.' – Khushbu Judal Mar 04 '20 at 08:30

1 Answers1

0

1st way

You can check iOS version and add custom status bar like this ->

override func viewDidAppear(_ animated: Bool) {
    if #available(iOS 13, *) {
        let statusBar = UIView(frame: (UIApplication.shared.keyWindow?.windowScene?.statusBarManager?.statusBarFrame)!)
        statusBar.backgroundColor = .systemBackground
        UIApplication.shared.keyWindow?.addSubview(statusBar)
    }
}

2nd way

You can customize UINavigationBarAppearance

if #available(iOS 13.0, *) {
    let navBarAppearance = UINavigationBarAppearance()
    navBarAppearance.configureWithOpaqueBackground()
    navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
    navBarAppearance.backgroundColor = <yourColor>
    navigationBar.standardAppearance = navBarAppearance
    navigationBar.scrollEdgeAppearance = navBarAppearance
}
emrcftci
  • 3,355
  • 3
  • 21
  • 35