简体   繁体   中英

hidesBarsOnSwipe for childView

I have the following (simplified) hierarchy: UINavigationController -> UIViewController -> UITableViewController . I would like to hide the navigation bar when I scroll my tableview using hidesBarsOnSwipe . 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.

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];

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM