在CollectionView中滚动tableView时隐藏NavigationBar?

muh*_*sva 10 uinavigationbar uitableview ios uicollectionview swift

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

And*_*ndy 23

从iOS 8开始,您就可以使用了

self.navigationController?.hidesBarsOnSwipe = true
Run Code Online (Sandbox Code Playgroud)

当然,这需要您的ViewController嵌入在NavigationController中.NavigationController的所有子VC都将继承此行为,因此您可能希望启用/禁用它viewWillAppear.您还可以在故事板中的导航控制器上设置相应的标志.

Storyboard中导航控制器的屏幕截图


Ana*_*mje 15

每当您想要滚动表格视图时,您可以使用一些git库进行滚动导航栏.滚动顶部到底部/底部到顶部它会自动调整导航栏.

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

迅速

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    if let navigationController = self.navigationController as? ScrollingNavigationController {
        navigationController.followScrollView(tableView, delay: 50.0)
    }
}
Run Code Online (Sandbox Code Playgroud)

目标 - C.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [(ScrollingNavigationController *)self.navigationController followScrollView:self.tableView delay:50.0f];
}
Run Code Online (Sandbox Code Playgroud)

它有一些委托方法有助于管理与滚动和导航相关的所有这些.

AMScrollingNavbar点击此处查看

在此输入图像描述

我认为这对你有帮助.


小智 8

尝试这个:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
    if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
        navigationController?.setNavigationBarHidden(true, animated: true)
    } else {
        navigationController?.setNavigationBarHidden(false, animated: true)
    }
}
Run Code Online (Sandbox Code Playgroud)


Vai*_*wad 5

创建一个@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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

at - viewwilldisappear或控制器移动到另一个控制器,因为这可能导致下一个视图控制器导航栏消失.