如何防止状态栏与 UINavigationController 上设置的 hidesBarsOnSwipe 内容重叠? [英] How to prevent status bar from overlapping content with hidesBarsOnSwipe set on UINavigationController?

查看:15
本文介绍了如何防止状态栏与 UINavigationController 上设置的 hidesBarsOnSwipe 内容重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 iOS 8 中添加的新功能 - 在用户滚动表格视图时隐藏导航栏(类似于移动 Safari 所做的).我在 UITableViewControllerviewDidAppear 方法中将 UINavigationController 的属性 hidesBarsOnSwipe 设置为 YES>:

I'm trying to use the new feature added in iOS 8 - hiding the navigation bar while user is scrolling the table view (similar to what mobile Safari does). I'm setting the property hidesBarsOnSwipe of UINavigationController to YES in viewDidAppear method of UITableViewController:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    if([self.navigationController respondsToSelector:@selector(hidesBarsOnSwipe)]) {
        self.navigationController.hidesBarsOnSwipe = YES;
    }
}

滚动视图时导航栏隐藏.到现在为止还挺好.但是状态栏仍然可见,我的表格视图内容通过它显示出来,看起来很难看:

The navigation bar hides when the view is being scrolled. So far so good. But the status bar is still visible and my table view contents show through it, which looks ugly:

我尝试将 edgesForExtendedLayout 设置为 UIEdgeRectNone 或调整表格视图的 contentInset ,但没有帮助.是否有任何其他解决方案可以将状态栏与导航栏一起隐藏,或者使其不透明?

I tried setting edgesForExtendedLayout to UIEdgeRectNone or adjusting the contentInset of the table view, but it didn't help. Is there any other solution to hide the status bar along with the navigation bar, or make it opaque?

推荐答案

基于 anas 的回答,我有一个可行的解决方案(我假设 tableViewController 是你的 UITableViewController实例):

Building off of anas' answer, I have a working solution (I'm assuming tableViewController is your UITableViewController instance):

UINavigationController 子类中(或者也可能来自 tableViewController):

In a UINavigationController subclass (or also potentially from tableViewController):

- (void)viewDidLoad {
    if ([self respondsToSelector:@selector(barHideOnSwipeGestureRecognizer)]) {
        // iOS 8+
        self.hidesBarsOnSwipe = YES;
        [self.barHideOnSwipeGestureRecognizer addTarget:self action:@selector(swipe:)];
    }
}

- (void)swipe:(UISwipeGestureRecognizer *)recognizer {
    BOOL shouldHideStatusBar = self.navigationController.navigationBar.frame.origin.y < 0;
    tableViewController.hideStatusBar = shouldHideStatusBar;
    [UIView animateWithDuration:0.2 animations:^{
        [tableViewController setNeedsStatusBarAppearanceUpdate];
    }];
}

在您的 tableViewController 中:

@property(nonatomic, getter = shouldHideStatusBar) BOOL hideStatusBar;

- (BOOL)prefersStatusBarHidden {
    return [self shouldHideStatusBar];
}

如果这对您不起作用,请告诉我.一些不明显的事情:

Let me know if this doesn't work for you. A few non-obvious things:

  • self.navigationController.navigationBar.frame.origin.y 隐藏时为-44(导航栏的负高度),可见时为20(状态栏的高度).即使在动画期间也没有中间值,因此负值 == 隐藏,非负值 == 可见.
  • 子视图控制器是查询状态栏是否应该隐藏的控制器.在我的例子中,我在 UITabBarControllerUINavigationController 中有一个 UIViewController,直到我覆盖 prefersStatusBarHiddenUIViewController 上.
  • 由于隐藏的状态栏没有框架,除非您将 setNeedsStatusBarAppearanceUpdate 调用封装在动画块中,否则您的内容可能会向上抖动 20 点.
  • 希望语法正确;我从我的 Swift 代码中反向移植了它.
  • self.navigationController.navigationBar.frame.origin.y was -44 (the negative height of the navigation bar) when hidden, and 20 (the height of the status bar) when visible. There was no in-between, even during animations, so a negative value == hidden and a nonnegative value == visible.
  • The child view controller is the one queried for whether or not the status bar should be hidden. In my case, I have a UIViewController within a UINavigationController within a UITabBarController, and it didn't work until I overrode prefersStatusBarHidden on the UIViewController.
  • Since a hidden status bar has no frame, your content might jerk upwards 20 points unless you wrap the call to setNeedsStatusBarAppearanceUpdate in an animation block.
  • Hopefully the syntax is correct; I backported this from my Swift code.

这篇关于如何防止状态栏与 UINavigationController 上设置的 hidesBarsOnSwipe 内容重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆