【问题标题】:How to refresh preferredStatusBarStyle in one ViewController for two or more times如何在一个ViewController中刷新preferredStatusBarStyle两次或多次
【发布时间】:2016-10-08 08:51:20
【问题描述】:
我的旧代码:
- (void)setStatusBarLight:(BOOL)value {
if (value) {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
} else {
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}
}
现在不推荐使用 setStatusBarStyle。 Apple 说,我应该使用 preferredStatusBarStyle。但这只会调用一次。但是在我的选项状态栏必须在此视图控制器中的某些用户操作后更改颜色。
我能做什么?
【问题讨论】:
标签:
xcode
colors
ios9
statusbar
【解决方案1】:
这里有一个解决方案:
[self setNeedsStatusBarAppearanceUpdate];
并且查看基于控制器的状态栏外观在Info.plist中设置为YES
【解决方案2】:
这里是一个视图控制器的例子,它有一个深色的 navBar 和白色的 statusBar 文本,并呈现一个带有浅色的 navBar 和黑色 statusBar 文本的视图控制器:
vc1(深色导航栏,白色状态栏文本)
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleDefault;
}
vc2(浅色导航栏,黑色状态栏文字)
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self setNeedsStatusBarAppearanceUpdate];
}
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
打开/显示 vc2 和 statusBar 文本将改变其颜色
- (void)openVC2 {
UIViewController *vc = [[UIViewController alloc]init];
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc];
[nvc.navigationBar setNavBarColor:COLOR_gray_light ShadowColor:[UIColor clearColor]];
[self presentViewController:nvc animated:YES completion:nil];
}
info: setNavBarColor:ShadowColor: 是我的类别中的自定义方法
不要忘记在 info.plist 上将 View controller-based status bar appearance 设置为 YES。