需求:全局黑色状态栏,某个页面白色状态栏
有两种实现方式:
第一种:全局设置状态栏颜色
1.在 info.plist 添加View controller-based status bar appearance 设置为NO
2 在AppDelegate.m 的方法中如下设置,默认会显示黑色
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //设置全局状态栏颜色为黑色 [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; //设置全局状态栏颜色为白色 // [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; return YES; } |
3.在其他页面设置状态栏的颜色
-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent; }
-(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault; } |
第二种:局部设置状态栏颜色
1.在 info.plist 添加View controller-based status bar appearance 设置为YES
2.控制器基类里
.h里
// 设置当前控制器的状态栏style @property(assign, nonatomic) BOOL mWhiteStausBar; |
.m里
- (void)setMWhiteStausBar:(BOOL)mWhiteStausBar { if (_mWhiteStausBar == mWhiteStausBar) { return; } _mWhiteStausBar = mWhiteStausBar;
[self setNeedsStatusBarAppearanceUpdate]; }
- (UIStatusBarStyle)preferredStatusBarStyle{ return self.mWhiteStausBar ? UIStatusBarStyleLightContent:UIStatusBarStyleDefault; } |
某个页面这样调用
self.mWhiteStausBar = YES; |