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

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

问题描述

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

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(状态栏的高度)。即使在动画期间也没有中间,所以负值==隐藏和非负值==可见。

  • 子视图控制器是查询是否为状态栏应该隐藏。在我的情况下,我在 UITabBarController UINavigationController 中有一个 UIViewController $ c>,直到我在 UIViewController 上覆盖 prefersStatusBarHidden 后它才能工作。

  • 由于隐藏状态栏没有框架,除非您在动画块中将调用包装到 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天全站免登陆