状态栏 就是指的最上面的20像素高的部分。状态栏分前后两部分,前景部分:就是指的显示电池、时间等部分;背景部分:就是显示黑色或者图片的背景部分;设置statusBar的【前景部分】简单来说,就是设置显示电池电量、时间、网络部分标示的颜色,这里只能设置两种颜色:默认的黑色(UIStatusBarStyleDefault),白色(UIStatusBarStyleLightContent)。
- plist设置statusBar
info.plist中- Status bar style: UIStatusBarStyleLightContent
- View controller-based status bar appearance: NO
- Status bar is initially hidden: NO
- 代码里
// <=9.0版本可用 [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [UIApplication sharedApplication].statusBarHidden = YES; // >9.0版本 在VC中实现以下两个方法即可 /** 设置状态栏的风格(系统的方法) */ - (UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } /** 设置状态栏是否隐藏:YES隐藏,NO不隐藏。(系统的方法) */ - (BOOL)prefersStatusBarHidden { return NO; } // 状态栏背景颜色的设置 1. 自己写一个方法,在VC中调用 /** 设置状态栏背景颜色(自己的方法) */ - (void)setStatusBarBackgroundColor:(UIColor *)color { UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"]; if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) { statusBar.backgroundColor = color; } } 2. 自己绘制一个view /** 自定义一个view替代bar的背景图(自己的方法) */ - (void)initCustomBar { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, -20, [UIScreen mainScreen].bounds.size.width, 40)]; view.backgroundColor = [UIColor orangeColor]; [self.view addSubview:view]; }