本期开发中, 遇到要修改特定UIViewController的UIStatusBar.
项目用的是:
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
这种方法已经废弃.
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") __TVOS_PROHIBITED;
虽然还能用,但是在UIViewController生命周期哪个时间调用才是正确,而不会影响到其他UIViewController?
就在这时懒癌发作, 决定改成系统推荐的方式来管理UIStatusBar.
即UIViewController需要时,重写以下方法:
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault
- 第一步,修改info.plist文件
找到View controller-based status bar appearance
选项,如下图:
将值改为YES,或者直接删除改选项(默认值就是YES).
- 第二步,实现'preferredStatusBarStyle'方法
- (UIStatusBarStyle)preferredStatusBarStyle{
return UIStatusBarStyleLightContent;
}
Run, 然并卵, 打个断点, 发现并没有调用.
搜索发现:
还是没用.
后面事实证明, 完全多余. 如果你Xcode有相关代码可以安心删除.
诸如UINavigationController, UITabBarController 等系统UIViewController容器类, Apple会自己搞定,不用开发者操心.
// Override to return a child view controller or nil. If non-nil, that view controller's status bar appearance attributes will be used. If nil, self is used. Whenever the return values from these methods change, -setNeedsUpdatedStatusBarAttributes should be called.
#if UIKIT_DEFINE_AS_PROPERTIES
@property(nonatomic, readonly, nullable) UIViewController *childViewControllerForStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
@property(nonatomic, readonly, nullable) UIViewController *childViewControllerForStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
#else
- (nullable UIViewController *)childViewControllerForStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (nullable UIViewController *)childViewControllerForStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
#endif
但是这两个API的确提醒了我, 应该是Child ViewController的问题.
于是从RootViewController开始查找, 发现 RootViewController 是一个UIViewController, 而UITabBarController是作为他的一个Child ViewController并完全盖住它. 估计就是这里传递断了.
于是新建一个RootViewController分类:
//这里RootViewController 是LMAppMainVC
//tabBarVC是UITabBarController
@implementation LMAppMainVC (LMStatusBarStyle)
- (UIViewController *)childViewControllerForStatusBarStyle{
return self.tabBarVC;
}
- (UIViewController *)childViewControllerForStatusBarHidden{
return self.tabBarVC;
}
@end
Run, 然后这次OK.棒棒哒.
简单讲讲, 从App启动开始UIStatusBar是怎么展示的.
- App启动时的样式,在下面这里设置:
- App启动后,是一个传递链条.
首先RootViewController开始,如果RootViewController是系统容器类的ViewController, 如UINavigationController, UITabBarController 等会自动传递到当前正在显示的ViewController.
如果ViewController实现了以下方法:
- (nullable UIViewController *)childViewControllerForStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
- (nullable UIViewController *)childViewControllerForStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
会按照实现的方法来传递.
主要是,当我们自己添加的Child ViewController, 就需要我们去实现这两个方法来往上传递.
当Present一个ViewController的时候,系统也会自己往上传递,不需要我们操心.
当然我们要注意non-full screen的情况,如下:
// This controls whether this view controller takes over control of the status bar's appearance when presented non-full screen on another view controller. Defaults to NO.
@property(nonatomic,assign) BOOL modalPresentationCapturesStatusBarAppearance NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
当已经无法再向上传递的时候, 系统会调用当前ViewController的下面两个方法,来展示UIStatusBar.
- (UIStatusBarStyle)preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault
- (BOOL)prefersStatusBarHidden NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to NO
// Override to return the type of animation that should be used for status bar changes for this view controller. This currently only affects changes to prefersStatusBarHidden.
- 手动灵活改变UIStatusBar
可以调用下面方法:
// This should be called whenever the return values for the view controller's status bar attributes have changed. If it is called from within an animation block, the changes will be animated along with the rest of the animation block.
- (void)setNeedsStatusBarAppearanceUpdate NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED;
先设置好显示不同样式条件返回:
- (UIStatusBarStyle)preferredStatusBarStyle{
if (self.customNavigationBar.separateLine.hidden) {
return UIStatusBarStyleLightContent;
} else {
return UIStatusBarStyleDefault;
}
}
然后在需要的时候调用:
[self setNeedsStatusBarAppearanceUpdate];
重点东西就这么多.
解决问题期间搜索到错误的答案,也给提个醒. 具体那个错误答案,戳我
如下图:
按照上面实现分类后, present 出来的UIViewController
的UIStatusBar
正常.
- (void)dismissViewControllerAnimated: (BOOL)flag completion: (void (^ __nullable)(void))completion NS_AVAILABLE_IOS(5_0);
后就变不回来了.
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController; // Return modal view controller if it exists. Otherwise the top view controller.
原因是dismissViewControllerAnimated
调用后立马就引发系统刷新状态栏, 但是这时被dismiss的ViewController没有立马消失, 所以visibleViewController依然是这个未消失的ViewController,导致系统调用了错误的preferredStatusBarStyle
.
提示一点系统默认是UIStatusBarStyleDefault
, 如果App需要默认的UIStatusBarStyleLightContent
.
建个分类改改就OK:
@implementation UIViewController (LMStatusBarStyle)
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
@end
OK, UIStatusBar
的正确使用姿势暂时就这么多.
2017.10.14 更新一个坑
self.navigationController.navigationBarHidden = NO
如果导航栏是隐藏的,然后再显示,有可能导致显示错误.
上面代码会引发状态栏的刷新,但是self.navigationController
不会自动向viewControllers
传递,而是自动调用- (UIStatusBarStyle)preferredStatusBarStyle
方法.
所以需要我们手动实现传递方法:
- (UIViewController *)childViewControllerForStatusBarStyle;
- (UIViewController *)childViewControllerForStatusBarHidden;
建立UINavigationController分类
@implementation UINavigationController (StatusBar)
- (UIViewController *)childViewControllerForStatusBarStyle{
return self.viewControllers.lastObject;
}
- (UIViewController *)childViewControllerForStatusBarHidden{
return self.viewControllers.lastObject;
}
@end
2017.10.15 更新一个坑
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
设置barStyle
为UIBarStyleBlack
,会导致navigationController
的 preferredStatusBarStyle
默认值 变为 UIStatusBarStyleLightContent
如果没有设置任何UIStatusBarStyleLightContent
, statusBar却莫名其妙变为UIStatusBarStyleLightContent
, 那极有可能barStyle
被设置为UIBarStyleBlack
.
( 不知道是不是UITabBarController也具备相同特性的)
demo还是上面那个,可以打开下面注释测试下:
//改变navigationController 的 preferredStatusBarStyle 默认值 变为 UIStatusBarStyleLightContent
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;