ios - 当 UITableView 嵌入到 UIPageViewController 中时,如何使用 hidesBarsOnSwipe 隐藏 NavigationBar?

标签 ios swift uitableview cocoa-touch uinavigationcontroller

Apple 添加了这个 hidesBarsOnSwipe 助手,当我们在项目列表中上下滚动时,它非常适合触发 NavigationBar 隐藏/显示操作。

我有一个 UIPageViewController,里面有很多 tableView。 当我在 UITableView(s) 中向上/向下滚动时,我想以完全相同的方式显示/隐藏导航栏 hidesBarsOnSwipe

不幸的是,当我在 UITableView 类中设置以下代码时:

  rootNavViewCtrl?.hidesBarsOnSwipe = true
  rootNavViewCtrl?.barHideOnSwipeGestureRecognizer.addTarget(self, action: #selector(didHideOnSwipe))

  func didHideOnSwipe() {
    log.debug("==== hide on swipe ====")
  }

它不会触发该功能。 它实际上仅在我开始水平滚动滑动时触发该功能(我猜 UIPageViewController 触发了手势识别器)然后通过对角向上或向下的运动结束它......

有人对此有一个干净的解决方案吗?

最佳答案

您可以将此代码转换为 Swift。我提供的是 Obj-C 语法。

在包含 tableView 的 View Controller 中,覆盖一些 UIScrollView 委托(delegate):

BOOL canHideOrShowNavBar;
NSInteger lastContentOffset;

 #pragma mark - UIScrollView Delegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.lastContentOffset > scrollView.contentOffset.y && canHideOrShowNavBar){
        if(self.parentViewController.navigationController.navigationBarHidden){
            [self.parentViewController.navigationController setNavigationBarHidden:NO animated:YES];
        }
    }else if (self.lastContentOffset < scrollView.contentOffset.y && canHideOrShowNavBar){
        if(!self.parentViewController.navigationController.navigationBarHidden){
            [self.parentViewController.navigationController setNavigationBarHidden:YES animated:YES];
        }
    }
    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    canHideOrShowNavBar = YES;
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView
                 withVelocity:(CGPoint)velocity
          targetContentOffset:(inout CGPoint *)targetContentOffset {
    canHideOrShowNavBar = NO;
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {
    if([self.parentViewController.navigationController isNavigationBarHidden])
        [self.parentViewController.navigationController setNavigationBarHidden:NO animated:YES];
    return YES;
}

如果在 UIPageViewController 下有多个 View Controller 也有 UITableView,您可以将这些代码放在 UIViewController 基子类中以防止冗余。


同时,我希望有人也能启发我,为什么当 UITableView 在 UIPageViewController 的 childViewController 中时 hidesBarsOnSwipe 不起作用。

UINavigationController //Initiate
  | 
UIPageViewController 
  |                   |                   |
UIViewControllerA   UIViewControllerB   UIViewControllerC
  |             ↘
UITableView  // <self.navigationController.canHideOrShowNavBar = YES; is not working when scrolling UITableView>

关于ios - 当 UITableView 嵌入到 UIPageViewController 中时,如何使用 hidesBarsOnSwipe 隐藏 NavigationBar?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39199869/

相关文章:

ios - Firebase 云消息传递 - iOS 角标(Badge)

android - 完全独立的 Rails 移动应用程序,编译为 native 应用程序

ios - 如何绘制 UIView 的一部分,因为它在 UICollectionView 中变得可见?

swift - TableView 布局未随设备方向变化而更新

ios - 当文本超出空间时使 UITableViewCell 展开

ios - 如何清理ios app tmp文件夹

arrays - 如果使用 IntroSort 算法,Swift Array.sort() 如何比元组更快地对整数进行排序? Swift 对整数的排序方式不同吗?

使用 Realm 和 RAC 进行 Swift 单元测试

ios - 从 Firebafirebase 崩溃报告 OLD 迁移到 Firebase/Crashlytics

ios - tableView如何:cellForRowAtIndexPath: protocol method updating cell content in Cocoa Touch?