改变状态栏的前景颜色,开发中,有时的需求是状态栏的文字需要设置成白色的,不同的界面又不成黑色的。这时我们要怎么设置呢?
第一步:在 info文件 添加 View controller-based status bar appearance 设置为no,
第二步:在视图将要出现的方法中修改状态栏的状态:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated ];
// 在视图将要出现时 status bar 状态栏 设置成默认
// 将状态栏变成白色 在 info文件 添加 View controller-based status bar appearance 设置为no
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
}
<img src="https://img.haomeiwen.com/i1772228/467c09219391e5cf.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = 500 />
第三步:在视图将要离开时,恢复默认的状态:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// 在视图将要离开 status bar 状态栏 设置成默认
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
<img src="https://img.haomeiwen.com/i1772228/c61fdafce02f55f4.jpeg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width = 500 />
设置状态栏的背景颜色
转自:http://www.jianshu.com/p/5c09c2700038
//设置状态栏颜色
- (void)setStatusBarBackgroundColor:(UIColor *)color {
UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
statusBar.backgroundColor = color;
}
}
-(void)viewWillAppear:(BOOL)animated{
[self setStatusBarBackgroundColor:[UIColor redColor]];
[UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
}
--->!!!同时别忘了在info plist里面将View controller-based status bar appearance设置成NO,(默认是YES)
状态栏背景图片
网友评论