解决隐藏 UIStatusBar 导致 UINavigationBar 抓狂的问题

1,883 阅读1分钟
原文链接: github.com

官方提供了一套 API 来控制状态栏的显示和隐藏

- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation;

- (UIViewController *)childViewControllerForStatusBarHidden;

- (BOOL)prefersStatusBarHidden;

- (void)setNeedsStatusBarAppearanceUpdate;

这一套规则在没有使用 UINavigationBar 之前,是正常的,生活是美好的。但是一旦涉及到 UINavigationBar, 就令人抓狂了,经历过的都懂,无须多言。

本库因此而生,解决有 UINavigationBar 时,隐藏状态栏带来的各种问题。

Usage

使用 hbd_statusBarHidden 取代 prefersStatusBarHidden 使用 hbd_setNeedsStatusBarHiddenUpdate 取代 setNeedsStatusBarAppearanceUpdate

在需要隐藏状态栏的控制器中编写如下代码

#import <HBDStatusBar/UIViewController+StatusBar.h>

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.hbd_statusBarHidden = YES;
}

或者

- (BOOL)hbd_statusBarHidden {
    return YES;
}

这是声明式 API,其它不需要隐藏状态栏的页面什么也不需要做。

如果需要动态隐藏或显示状态栏,在设置 hbd_statusBarHidden 后调用 hbd_setNeedsStatusBarHiddenUpdate 即可。

本库可以和 UINavigationBar 友好相处

你仍然可以通过 preferredStatusBarUpdateAnimation 来指定状态栏显示或隐藏时的动画,仍然可以通过 childViewControllerForStatusBarHidden 来指定由哪个子控制器来决定是否隐藏状态栏

如果你遇到 UINavigationBar 的相关问题,请访问 HBDNavigationBar

Installation

HBDStatusBar is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'HBDStatusBar'

Author

listenzz@163.com

License

HBDStatusBar is available under the MIT license. See the LICENSE file for more info.