9

I use the navigationController, and i hide the navigationBar, how to set statusBar color on iPhoneX? I do not want to set self.view.backgroundColor

iPhoneX notch color

enter image description here

Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44
Jerny
  • 151
  • 1
  • 8
  • see this https://stackoverflow.com/questions/46222041/how-to-get-a-black-statusbar-on-iphone-x-on-ios-11 – user6788419 Dec 04 '17 at 05:28
  • thank you. maybe i have to set color of the background view. – Jerny Dec 05 '17 at 04:11
  • Refer https://stackoverflow.com/questions/46354227/how-to-change-the-bottom-edge-color-on-the-iphone-x-programmatically – ABM Mar 05 '18 at 06:25

4 Answers4

4
UIView *statusBar = (UIView *)[[UIApplication sharedApplication] valueForKey:@"statusBar"];
statusBar.backgroundColor = [UIColor greenColor];
Jerny
  • 151
  • 1
  • 8
3

Here is a hacky trick to change/set background color for status bar during application launch or during viewDidLoad of your view controller.

extension UIApplication {

    var statusBarView: UIView? {
        return value(forKey: "statusBar") as? UIView
    }

}

// Set upon application launch, if you've application based status bar
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
        return true
    }
}


or 
// Set it from your view controller if you've view controller based statusbar
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        UIApplication.shared.statusBarView?.backgroundColor = UIColor.red
    }

}



Here is result:

enter image description here

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 1
    Method works great for me. A simplification for your first implementation would be `application.statusBarView?.backgroundColor = .red` since the instance of `UIApplication` is passed into the function there is no need to call your singleton instance. – NSGangster Jul 21 '18 at 14:41
  • 1
    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.' – jeet.chanchawat Oct 04 '19 at 03:46
2

Swift 4

func setStatusBarBackgroundColor(_ color: UIColor) {
    guard let statusBar = UIApplication.shared.value(forKeyPath: "statusBarWindow.statusBar") as? UIView else { return }
    statusBar.backgroundColor = color
}
Ahmadreza
  • 6,950
  • 5
  • 50
  • 69
  • 4
    *** 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.' – jeet.chanchawat Oct 04 '19 at 03:49
  • 1
    Crashes in IOS 13 / iPhone 11 Pro Max – Cerlin Oct 30 '19 at 10:35
  • Here is the answer for iOS 13: https://stackoverflow.com/a/58524665/7851379 – Denis Bystruev Oct 30 '19 at 14:36
0

In Interface Builder, I simply added a view at 0x0, 44px high and the same width as the top-level view, then set this view to the desired background color. This changed the background color of the status bar without affecting the view background color.

In my case, I needed something like this because the top half of my screen is one color and the bottom half is the other. I didn't see another way to make the status bar match the top color and the home button area match the bottom color.

This could easily be done programmatically if the extra view gets in the way on devices other than the iPhone X. In my case it doesn't cause problems on other devices.

arlomedia
  • 8,534
  • 5
  • 60
  • 108