本文共 1815 字,大约阅读时间需要 6 分钟。
StatusBar字体默认为黑色, 要想在工程中启动和视图中的状态栏都是白色,用下面的方法简单配置下就行。
1.设置Status Bar Style为Light. (Status bar style UIStatusBarStyleLightContent)
2.在plist文件中添加 View controller-based status bar appearance = NO。
如果是导航视图控制器(UINavigationController):
UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, Screen_Width, 20)];
statusBar.backgroundColor = [UIColor blackColor]; [self.navigationController.navigationBar addSubview:statusBar]; self.navigationController.navigationBar.barStyle = UIBarStyleBlack; self.navigationController.navigationBar.translucent = NO;如果是视图控制器(UIViewController) : 以下两种方法都可以
(1)
UIView *statusBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 20)];
statusBar.backgroundColor = [UIColor blackColor]; [self.view addSubview:statusBar];
(2)
// 设置状态栏颜色 - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } // 在viewWillAppear方法里调用下面方法 -(void)viewWillAppear:(BOOL)animated{ [self setStatusBarBackgroundColor:kMainColor]; }
// update
iOS9 之前 :
全局设置statusBar : step1. info.plist 里面加上View controller-based status bar appearance BOOL值设为NO (就是把控制器控制状态栏的权限给禁了,用UIApplication来控制。)step2.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent] [[UIApplication sharedApplication]setStatusBarHidden:YES]; 但是这种做法在iOS9不建议使用了,建议我们使用吧那个BOOL值设为YES,然后用控制器的方法来管理状态栏比如:- (UIStatusBarStyle)preferredStatusBarStyle
{ return UIStatusBarStyleLightContent; }如果iOS9之后, 想方便的全局控制statusBar的字体颜色或隐藏, 可以这样处理:
除了设置View controller-based status bar appearance为 NO外,还需要修改【General】—>【Deployment Info】—>【Status Bar Style】。