iOS / Swift – 向下/向上滚动时隐藏/显示UITabBarController

我对iOS开发很陌生。 现在我试图隐藏我的标签栏,当我向下滚动,当滚动向上的标签栏应该出现。 我想要像导航栏一样的animation。 对于导航栏,我只需单击属性检查器中的选项。 我看到了一些工具栏的例子,但我不能采用它的标签栏。

self.tabBarController?.tabBar.hidden = true只是隐藏我的self.tabBarController?.tabBar.hidden = true ,但它没有像导航控制器的animation。

这是我实际在生产应用程序中使用的代码。

它在Swift中 ,它也更新UITabBar.hidden var。

 func scrollViewWillBeginDragging(scrollView: UIScrollView) { if scrollView.panGestureRecognizer.translationInView(scrollView).y < 0{ changeTabBar(true, animated: true) } else{ changeTabBar(false, animated: true) } } 

您也可以使用其他callback方法:

 func scrollViewDidScroll(scrollView: UIScrollView) { ... } 

但是如果你这样select,那么你最多处理多个调用实际隐藏tabBar的帮助器方法。

然后你需要添加这个方法来animationTabBar的隐藏/显示。

 func changeTabBar(hidden:Bool, animated: Bool){ var tabBar = self.tabBarController?.tabBar if tabBar!.hidden == hidden{ return } let frame = tabBar?.frame let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!) let duration:NSTimeInterval = (animated ? 0.5 : 0.0) tabBar?.hidden = false if frame != nil { UIView.animateWithDuration(duration, animations: {tabBar!.frame = CGRectOffset(frame!, 0, offset)}, completion: { println($0) if $0 {tabBar?.hidden = hidden} }) } } 

基于Ariel的回答,我已经更新了Swift3的代码。 这对我的collections意见很好。

 override func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) { if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0 { changeTabBar(hidden: true, animated: true) }else{ changeTabBar(hidden: false, animated: true) } } func changeTabBar(hidden:Bool, animated: Bool){ let tabBar = self.tabBarController?.tabBar if tabBar!.isHidden == hidden{ return } let frame = tabBar?.frame let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!) let duration:TimeInterval = (animated ? 0.5 : 0.0) tabBar?.isHidden = false if frame != nil { UIView.animate(withDuration: duration, animations: {tabBar!.frame = frame!.offsetBy(dx: 0, dy: offset)}, completion: { print($0) if $0 {tabBar?.isHidden = hidden} }) } } 

您可以精确地控制UITabBar,方法是将您的类设置为scrollView的委托并在scrollViewDidScroll: method中实现滚动。

这里是我如何做我的应用程序的一个例子。 你可以很容易地修改你的需求。 包括一些帮助函数来获取UITabBar。

 #define LIMIT(__VALUE__, __MIN__, __MAX__) MAX(__MIN__, MIN(__MAX__, __VALUE__)) - (void)scrollViewDidScroll:(UIScrollView *)scrollView { CGFloat scrollOffset = scrollView.contentOffset.y; CGFloat scrollDiff = scrollOffset - self.previousScrollViewYOffset; CGFloat scrollHeight = scrollView.frame.size.height; CGFloat scrollContentSizeHeight = scrollView.contentSize.height + scrollView.contentInset.bottom; CGFloat scrollOffsetGlobal = scrollOffset + scrollView.contentInset.top; [self updateUITabBarY:[self UITabBarView].frame.origin.y + scrollDiff]; self.previousScrollViewYOffset = scrollOffset; } - (UITabBar*) UITabBarView { for(UIView *view in self.tabBarController.view.subviews) { if([view isKindOfClass:[UITabBar class]]) { return (UITabBar*) view; } } return nil; } - (void) updateUITabBarY:(CGFloat) y { UITabBar* tabBar = [self UITabBarView]; if(tabBar) { CGRect frame = tabBar.frame; frame.origin.y = LIMIT(y, [self UITabBarMiny], [self UITabBarMaxY]); tabBar.frame = frame; } } - (CGFloat) UITabBarMiny { return [UIScreen mainScreen].bounds.size.height - [self UITabBarView].frame.size.height - [[UIApplication sharedApplication] statusBarFrame].size.height + 20.0f; } - (CGFloat) UITabBarMaxY { return [UIScreen mainScreen].bounds.size.height; } 

这个答案是Ariel答案稍微修改,在用户滚动时添加animation。

 extension ViewController:UIScrollViewDelegate{ func scrollViewDidScroll(_ scrollView: UIScrollView) { if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{ //scrolling down changeTabBar(hidden: true, animated: true) } else{ //scrolling up changeTabBar(hidden: false, animated: true) } } func changeTabBar(hidden:Bool, animated: Bool){ let tabBar = self.tabBarController?.tabBar let offset = (hidden ? UIScreen.main.bounds.size.height : UIScreen.main.bounds.size.height - (tabBar?.frame.size.height)! ) if offset == tabBar?.frame.origin.y {return} print("changing origin y position") let duration:TimeInterval = (animated ? 0.5 : 0.0) UIView.animate(withDuration: duration, animations: {tabBar!.frame.origin.y = offset}, completion:nil) } }