iOS状态栏隐藏方法采坑

本文最后更新于:2 年前

一、Xcode配置

1、启动页

XcodeTARGETS里面General选项卡中有两个配置项如图

image_1.png

当修改这两个配置项后,工程plist文件中会出现两个相对应的键值对,值与General中配置一致,如图

image_2.png

  • 默认创建新工程时plist文件中是不会有这两个值的,只有修改过General选项卡的配置才会产生,可以手动在plist中添加
key value
Status bar is initially hidden APP启动页是否隐藏状态栏
Status bar style APP启动页状态栏样式,Gray style (default)或者UIStatusBarStyleLightContent

2、其他页面

除了启动页的状态栏可以通过配置实现,APP中其他页面的状态栏状态系统也提供了plist中的配置参数View controller-based status bar appearance,用来设置APP中其他页面的状态栏效果。

image_3

demo测试结果如下:

image_4

  • 只有当设置Status bar is initially hidden为YES时,才能通过设置View controller-based status bar appearance为NO来隐藏所有的状态栏
  • 刘海屏由于特殊构造,在隐藏状态栏后,顶部有SafeArea的距离存在

image_5

二、代码设置

一般我们在代码中控制个别页面没有状态栏,早期我们使用系统提供的全局设置方法来修改状态栏的效果,如下所示:

1
2
3
4
5
6
7
// Setting the statusBarStyle does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic) UIStatusBarStyle statusBarStyle NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") __TVOS_PROHIBITED;
- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController preferredStatusBarStyle]") __TVOS_PROHIBITED;

// Setting statusBarHidden does nothing if your application is using the default UIViewController-based status bar system.
@property(readwrite, nonatomic,getter=isStatusBarHidden) BOOL statusBarHidden NS_DEPRECATED_IOS(2_0, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;
- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation NS_DEPRECATED_IOS(3_2, 9_0, "Use -[UIViewController prefersStatusBarHidden]") __TVOS_PROHIBITED;

这些方法每次都是对全局产生效果,在iOS7以后,系统提供了其他的替代方法:

1
2
3
4
5
6
7
8
9
10
11
12
// These methods control the attributes of the status bar when this view controller is shown. They can be overridden in view controller subclasses to return the desired status bar attributes.
@property(nonatomic, readonly) UIStatusBarStyle preferredStatusBarStyle NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarStyleDefault
@property(nonatomic, readonly) 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.
@property(nonatomic, readonly) UIStatusBarAnimation preferredStatusBarUpdateAnimation NS_AVAILABLE_IOS(7_0) __TVOS_PROHIBITED; // Defaults to UIStatusBarAnimationFade

// 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;

// 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.
@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;

通过重写prefersStatusBarHidden的方法来返回相应的状态

1
2
3
- (BOOL)prefersStatusBarHidden {
return YES;
}

如果工程中存在UINavigationController或者UITabBarController,需要重写相应的childViewControllerForStatusBarHidden的方法

1
2
3
- (UIViewController *)childViewControllerForStatusBarHidden {
return self.topViewController;
}

使用demo测试如下:

image_6.png

image_7.png