在CollectionView中滚动tableView时隐藏NavigationBar?

我有collectionViewController和collectionViewCell包含TableView.CollectionView是水平布局。我想在滚动tableView时隐藏导航栏。 有没有什么想法。

你可以使用一些git库的滚动导航栏,每当你想滚动你的表视图/滚动从上到下/下到顶部它会自动调整你的导航栏。

你可以像这样使用这个代码来使用这个库

迅速

 override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) if let navigationController = self.navigationController as? ScrollingNavigationController { navigationController.followScrollView(tableView, delay: 50.0) } } 

目标 – C

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:50.0f]; } 

它有一些委托方法帮助pipe理所有这些相关的滚动和导航。

AMScrollingNavbar点击这里查看

在这里输入图像说明

我认为这对你有帮助。

由于iOS 8,你可以使用

 self.navigationController?.hidesBarsOnSwipe = true 

这当然需要你的ViewController被embedded到一个NavigationController中。 NavigationController的所有子VC都会inheritance这个行为,所以你可能想在viewWillAppear启用/禁用它。 您还可以在故事板中的导航控制器上设置相应的标志。 故事板中的导航控制器的屏幕截图

创build一个@property(assign,nonatomic)CGFloat currentOffset;

 -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView { scrollView = self.collectionProductView; _currentOffset = self.collectionProductView.contentOffset.y; } -(void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat scrollPos = self.collectionProductView.contentOffset.y ; if(scrollPos >= _currentOffset ){ //Fully hide your toolbar [UIView animateWithDuration:2.25 animations:^{ [self.navigationController setNavigationBarHidden:YES animated:YES]; }]; } else { //Slide it up incrementally, etc. [self.navigationController setNavigationBarHidden:NO animated:YES]; } } 

请不要忘记再次粘贴[self.navigationController setNavigationBarHidden:NO animated:YES];

在viewwilldisappear或每当控制器移动到另一个,因为这可能会导致下一个视图控制器导航栏消失。

  func scrollViewDidScroll(_ scrollView: UIScrollView) { // var navigationBarFrame = self.navigationController!.navigationBar.frame let currentOffset = scrollView.contentOffset if (currentOffset.y > (self.lastContentOffset?.y)!) { if currentOffset.y > 0 { initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y)) } else if scrollView.contentSize.height < scrollView.frame.size.height { initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y)) } } else { if currentOffset.y < scrollView.contentSize.height - scrollView.frame.size.height { initial = initial + fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y)) } else if scrollView.contentSize.height < scrollView.frame.size.height && initial < maxPlus { initial = initial - fabs(CGFloat(currentOffset.y - self.lastContentOffset!.y)) } } if (initial <= maxMinus){ initial = maxMinus self.tableviewTopConstrin.constant = 0 UIView.animate(withDuration: 0.4, animations: { self.view.layoutIfNeeded() }) }else if(initial >= maxPlus){ initial = maxPlus self.tableviewTopConstrin.constant = 70 UIView.animate(withDuration: 0.4, animations: { self.view.layoutIfNeeded() }) }else{ } self.lastContentOffset = currentOffset; }