iOS 8 - Set Status Bar Color (when your UINavigati

2019-03-01 15:19发布

问题:

I am using a view pager in my app (specifically ICViewPager). To make the View Pager blend into the Navigation bar like so:

I had to put these lines of code in my AppDelegate.m

[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];

However, this causes the status bar not to be the same color as my navigation bar.

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

回答1:

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.



回答2:

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



回答3:

You can change style of status bar

typedef enum : NSInteger {
    UIStatusBarStyleDefault,
    UIStatusBarStyleLightContent,

    UIStatusBarStyleBlackTranslucent,
    UIStatusBarStyleBlackOpaque
} UIStatusBarStyle;

//
@implementation ViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
     return UIStatusBarStyleLightContent;
}
@end