ios 11 如何在滚动collectionView 时隐藏navigationBar?

How to hide navigationBar when scrolling collectionView in ios 11?

在iOS 11 如果我们设置为隐藏,当滚动tableView时searchBar会隐藏。向上滚动 collectionView 时如何隐藏 searchBar、navigationBar 和 tabBar?向下滚动时取消隐藏它们?感谢您的帮助...

  1. 在您的 UIViewController 中子类 UIScrollViewDelegate(即 class ViewController: UIViewController, UIScrollViewDelegate { codes... }
  2. 实现scrollViewDidScroll委托方法

    func scrollViewDidScroll(scrollView: UIScrollView) { 
        let pan = scrollView.panGestureRecognizer
        let velocity = pan.velocityInView(scrollView).y
        if velocity < -5 { 
            self.navigationController?.setNavigationBarHidden(true, animated: true) 
            self.navigationController?.setToolbarHidden(true, animated: true)
        } else if velocity > 5 {
            self.navigationController?.setNavigationBarHidden(false, animated: true)
            self.navigationController?.setToolbarHidden(false, animated: true)
        }
    }
    

正在将 KTang 接受的答案翻译成 Objective-C:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
    CGFloat velocity = [pan velocityInView:scrollView].y;

    if (velocity < -5) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
    } else if (velocity > 5) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
    }
}