iOS 8 - Set Status Bar Color (when your UINavigati

2019-03-01 14:59发布

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

enter image description here

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?

3条回答
ら.Afraid
2楼-- · 2019-03-01 15:30

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.

查看更多
三岁会撩人
3楼-- · 2019-03-01 15:33

You can change style of status bar

typedef enum : NSInteger {
    UIStatusBarStyleDefault,
    UIStatusBarStyleLightContent,

    UIStatusBarStyleBlackTranslucent,
    UIStatusBarStyleBlackOpaque
} UIStatusBarStyle;

//
@implementation ViewController

- (UIStatusBarStyle)preferredStatusBarStyle {
     return UIStatusBarStyleLightContent;
}
@end
查看更多
我命由我不由天
4楼-- · 2019-03-01 15:48
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

查看更多
登录 后发表回答