在info.plist中,将View controller-based status bar appearance设置不同值,有不同的处理方法
一、将View controller-based status bar appearance设为YES或者没有设置
View controller-based status bar appearance的默认值就是YES,如果View controller-based status bar appearance为YES,那么下面这个方法无效
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
需要在VC中做一些处理
1、在VC中重写preferredStatusBarStyle
- (UIStatusBarStyle) preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}
2、在VC中调用
[
self
setNeedsStatusBarAppearanceUpdate
]方法来更新状态栏字体,该方法会触发VC中的preferredStatusBarStyle方法。
但是,当VC在UINavigationController中时,上面方法无效,VC中的preferredStatusBarStyle方法不会被调用。
原因在于preferredStatusBarStyle在UINavigationController被调用,VC中的preferredStatusBarStyle方法不会被调用。
解决办法有2个:
方法1:设置self.navigationController.navigationBar的barStyle,该属性会影响状态栏的字体何背景色。
// 状态栏文字颜色将改变成白色
// 导航栏的背景色将改变成黑色。
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 状态栏文字颜色将改变成黑色
// 导航栏的背景色将改变成白色
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
方法2:在UINavigationController中重写preferredStatusBarStyle方法。
- (UIStatusBarStyle)preferredStatusBarStyle
{
UIViewController* visibleViewController = self.visibleViewController;
return [visibleViewController preferredStatusBarStyle];
}
二、将View controller-based status bar appearance设为NO
状态栏字体的颜色只由下面属性设定,默认是黑色:
// Dark content, for use on light backgrounds
// default is UIStatusBarStyleDefault
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
1、在info.plist中,将View controller-based status bar appearance设为NO。
2、在App Delegate中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
return YES;
}
3、在VC中处理状态栏颜色字体
-(void)viewWillAppear:(BOOL)animated
{
// Super viewWillAppear
[super viewWillAppear:animated];
_previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:animated];
// Super viewWillDisappear
[super viewWillDisappear:animated];
}
在VC中更完整的代码为
@interface ViewController ()
@property (nonatomic,assign) UIStatusBarStyle previousStatusBarStyle;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated
{
// Super viewWillAppear
[super viewWillAppear:animated];
_previousStatusBarStyle = [[UIApplication sharedApplication] statusBarStyle];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:animated];
// Super viewWillDisappear
[super viewWillDisappear:animated];
}
@end