iOS 8 - Set Status Bar Color (when your UINavigationBar has a nil backgroud image)

孤街浪徒 提交于 2019-12-02 06:34:38

How do I set the color of my status bar to be the same as my navigation bar?

You don't. The status bar has no color. It is transparent. It is your job to make some view that has the desired color extend up to the top of the view, behind the status bar, so that that color shows through.

This view could be your window, for example, or the view controller's main view. Right now, it is white, and so we see the white color show through above the top of the navigation bar. If you give your window (or main view, or whatever the white thing is that we are seeing) the same color as the navigation bar, you'll get the effect you want.

Alternatively, you could adjust the size and position of the navigation bar itself so that it reaches to the top of the window and covers it.

override func viewWillAppear(animated: Bool) {
    //Status bar style and visibility
    UIApplication.sharedApplication().statusBarStyle = .LightContent

    //Change status bar color
    let statusBar: UIView = UIApplication.sharedApplication().valueForKey("statusBar") as! UIView
    if statusBar.respondsToSelector(Selector("setBackgroundColor:")) {
        statusBar.backgroundColor = customRed
    }
}

here you go

You can change style of status bar

typedef enum : NSInteger {
    UIStatusBarStyleDefault,
    UIStatusBarStyleLightContent,

    UIStatusBarStyleBlackTranslucent,
    UIStatusBarStyleBlackOpaque
} UIStatusBarStyle;

//
@implementation ViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
     return UIStatusBarStyleLightContent;
}
@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!