我正在尝试使用 iOS 8 中添加的新功能 - 在用户滚动表格视图时隐藏导航栏(类似于移动 Safari 所做的).我在 UITableViewController
的 viewDidAppear
方法中将 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(状态栏的高度).即使在动画期间也没有中间值,因此负值 == 隐藏,非负值 == 可见.UITabBarController
的 UINavigationController
中有一个 UIViewController
,直到我覆盖 prefersStatusBarHidden
在 UIViewController
上.setNeedsStatusBarAppearanceUpdate
调用封装在动画块中,否则您的内容可能会向上抖动 20 点.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.UIViewController
within a UINavigationController
within a UITabBarController
, and it didn't work until I overrode prefersStatusBarHidden
on the UIViewController
.setNeedsStatusBarAppearanceUpdate
in an animation block.这篇关于如何防止状态栏与 UINavigationController 上设置的 hidesBarsOnSwipe 内容重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!