有两种方法可以控制状态栏的颜色及可见性
一 使用UIApplication
相关方法
- 前提: 需要在
Info.plist
文件中设置View controller-based status bar appearance
为NO
. - 注意:IOS9已经把这个方法声明为废弃方法了.
- 代码如下
- (IBAction)changeColor:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
} else {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
}
}
- (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
if (sender.selectedSegmentIndex == 0) {
[[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];
} else {
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
}
}
二 使用UIViewController
相关方法
- 前提:需要在
Info.plist
文件中设置View controller-based status bar appearance
为YES
. 或者去掉这个键值对,默认为YES
. - 代码如下
- (IBAction)changeColor:(UISegmentedControl *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
- (IBAction)setHiddenStatus:(UISegmentedControl *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
#pragma mark - UIViewController 相关方法
- (UIStatusBarStyle)preferredStatusBarStyle
{
if (self.colorSegmentControl.selectedSegmentIndex == 0) {
return UIStatusBarStyleDefault;
}
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
if (self.hiddenSegmentControl.selectedSegmentIndex == 0) {
return NO;
}
return YES;
}
//状态栏隐藏或显示的动画
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationFade;
}
网友评论