By opening the mainstream app on your phone, it's easy to see that their status bar is the same background color as the navigation bar, such as and Instagram:
WECHAT. Png
Ins. Png
So today we're going to take a look at this little thing that's only 20 pixels tall, statusbar.
Uistatusbarstyle
The status bar has two display styles:
1.UIStatusBarStyleDefault
Default.png
2.UIStatusBarStyleLightContent
Lightcontent.png
So how does the background color add to it? Very simple, look at the code:
UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, self.view.frame.size.width, 20)];statusBar.backgroundColor = myColor;[self.navigationController.navigationBar addSubview:statusBar];
Read the code and know there's nothing to say.
To change the display style (foreground color) of the status bar, you need to overload the method in Viewcontoller:
- (UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}
However, the above method cannot be called directly , you need to use this method to refresh the status bar style, for example:
- (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self setNeedsStatusBarAppearanceUpdate];}
If you follow the above and set it up in your own viewcontroller, run to find your status bar or default state ... That's right, because it's far from easy. You need to be patient and look down ...
Uiviewcontrollerbasedstatusbarappearance
In info.plist , you can set whether the appearance of the status bar is based on the view controller, and the name of the key is uiviewcontrollerbasedstatusbarappearance. If not set then its default value is Yes, indicating that the view controller determines the style of the status bar, and if the value is set to NO, each view controller must explicitly use the UIApplication object to set the style of the status bar.
Hey ~ this time you will be puzzled, I follow what you said did not set the plist file Ah, so the default is Yes, then is the view controller to determine the status bar style ah, why not?
Childviewcontrollerforstatusbarstyle
When we call setneedsstatusbarappearanceupdate , the system calls Application.window.rootViewController 's Preferredstatusbarstyle method, not the Preferredstatusbarstyle method of the current controller. At this time, an important method will come in handy, that is:Childviewcontrollerforstatusbarstyle.
Childviewcontrollerforstatusbarstyle returns nil by default. So we need to rewrite this method.
Let's say your app Reagan view is a navigation controller:
[[UINavigationController alloc] initWithRootViewController:viewVontroller];
Well, we subclass a navigation controller zxnavigationcontroller, overriding its Childviewcontrollerforstatusbarstyle method:
@implementation ZXNavigationController- (UIViewController *)childViewControllerForStatusBarStyle{ return self.topViewController;}- (void)viewDidLoad { [super viewDidLoad];}
The above code means, do not call me the Preferredstatusbarstyle method of Uinavigationcontroller, but to call Navigationcontroller.topviewcontroller 's preferredstatusbarstyle method, so to write, The Preferredstatusbarstyle method of the currently displayed Uiviewcontroller can be guaranteed to be called to achieve the StatusBar foreground color.
Then in the application Didfinishlaunchingwithoptions: method to replace the Uinavigationcontroller Zxnavigationcontroller :
[[ZXNavigationController alloc] initWithRootViewController:viewVontroller];
Run A little bit and you'll find that it's a job.
If uiviewcontrollerbasedstatusbarappearance is set to NO, then you need to explicitly set the style of the status bar through the uiapplication object. :
self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:viewVontroller];//setStatusBarStyle从9.0开始不被推荐使用了:[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
At last
To say so much, simply and rudely summed up is: If you want to change the StatusBar display style, the uiviewcontrollerbasedstatusbarappearance set to NO , and then set the StatusBar Style through the uiapplication object.
Demo_git Address
iOS StatusBar detailed