【问题标题】:Hide NavigationBar when scrolling tableView in CollectionView?在 CollectionView 中滚动 tableView 时隐藏 NavigationBar?
【发布时间】:2016-06-23 09:02:19
【问题描述】:
我有 collectionViewController 和 collectionViewCell 包括 TableView。CollectionView 是水平布局。我想在滚动 tableView 时隐藏导航栏。有什么想法吗?
【问题讨论】:
标签:
ios
swift
uitableview
uicollectionview
uinavigationbar
【解决方案1】:
从 iOS 8 开始就可以使用
self.navigationController?.hidesBarsOnSwipe = true
这当然要求您的 ViewController 嵌入在 NavigationController 中。 NavigationController 的所有子 VC 都将继承此行为,因此您可能希望在 viewWillAppear 中启用/禁用它。
您还可以在故事板的导航控制器上设置相应的标志。
【解决方案2】:
当你想滚动你的表格视图/从上到下/从下到上滚动时,你可以使用一些 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];
}
它有一些委托方法有助于管理与滚动和导航相关的所有这些。
AMScrollingNavbar click here for see
我认为这对你有帮助。
【解决方案3】:
试试这个:
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
【解决方案4】:
创建一个
@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动画:YES];
at - viewwilldisappear 或当控制器移动到另一个时,因为这可能会导致下一个视图控制器导航栏消失。
【解决方案5】:
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;
}
【解决方案6】:
在 nao 的回答之上添加:
如果scrollview高度不够小,隐藏导航栏时会导致scrollview不可滚动。如果滚动视图变得不可滚动,则不会调用此函数并且导航栏将永远消失
func scrollViewDidScroll(_ scrollView: UIScrollView) {
let height = view.safeAreaLayoutGuide.layoutFrame.size.height
let scrolled = scrollView.panGestureRecognizer.translation(in: scrollView).y
if !(scrollView.visibleSize.height - height >= 90) {
if scrolled < 0 {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}
}
【解决方案7】:
其他解决方案存在一个错误,如果您将手指从屏幕上抬起并再次按住它,则会出现 navController...
这个方法更好:
// Show/Hide the NavigationBar when scrolling
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
if scrollView.contentOffset.y > -48 {
navigationController?.setNavigationBarHidden(true, animated: true)
} else {
navigationController?.setNavigationBarHidden(false, animated: true)
}
}