[英]hidesBarsOnSwipe for childView
I have the following (simplified) hierarchy: UINavigationController -> UIViewController -> UITableViewController
. 我有以下(简化)层次结构: UINavigationController -> UIViewController -> UITableViewController
。 I would like to hide the navigation bar when I scroll my tableview using hidesBarsOnSwipe
. 当我使用hidesBarsOnSwipe
滚动我的tableview时,我想隐藏导航栏。 What happens now is that the navigation bar hides whenever I scroll down, but it will not reappear when I scroll upwards. 现在发生的是每当我向下滚动时导航栏都会隐藏,但是当我向上滚动时它不会再出现。 This is what my code looks like: 这就是我的代码:
// Create a navigation controller and set as root view controller
// Enable hidesBarsOnSwipe
UINavigationController *navigationC = [UINavigationController new];
self.window.rootViewController = navigationC;
navigationC.hidesBarsOnSwipe = YES;
// Create a view controller to act as parent for the table view
UIViewController *parentVC = [UIViewController new];
[navigationC pushViewController:parentVC animated:NO];
// Create the table view controller
UITableViewController *tableVC = [UITableViewController new];
tableVC.tableView.dataSource = self;
// Add the table view as a subview to the parent view controller
[parentVC addChildViewController:tableVC];
[parentVC.view addSubview:tableVC.tableView];
[tableVC didMoveToParentViewController:parentVC];
This should work. 这应该工作。
First add UIScrollViewDelegate in your.h or .m file. 首先在your.h或.m文件中添加UIScrollViewDelegate。
Then add the following delegate methods. 然后添加以下委托方法。
#pragma mark - UIScrollViewDelegate Methods
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
self.lastContentOffsetY = scrollView.contentOffset.y;
}
- (void) scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
bool shouldHide = (scrollView.contentOffset.y > self.lastOffsetY);
[[self navigationController] setNavigationBarHidden:shouldHide animated:YES];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.