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

前端之家收集整理的这篇文章主要介绍了iOS / Swift – 向下/向上滚动时隐藏/显示UITabBarController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是iOS开发的新手.现在我正在尝试隐藏我的标签栏,当我向下滚动时,向上滚动标签栏应该出现.我想像导航栏一样动画这个动画.对于导航栏,我只需单击“属性”检查器中的选项.我看到了工具栏的一些示例,但我不能将其用于tabbar.

self.tabBarController?.tabBar.hidden = true只是隐藏我的tabbar,但它不像导航控制器那样动画.

解决方法

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

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

  1. func scrollViewWillBeginDragging(scrollView: UIScrollView) {
  2. if scrollView.panGestureRecognizer.translation(in: scrollView).y < 0{
  3. changeTabBar(hidden: true,animated: true)
  4. }
  5. else{
  6. changeTabBar(hidden: false,animated: true)
  7. }
  8. }

您还可以使用其他回调方法

  1. func scrollViewDidScroll(scrollView: UIScrollView) {
  2. ...
  3. }

但是如果你选择这样,那么你必须处理多个实际隐藏tabBar的辅助方法调用.

然后你需要添加这个方法来动画tabBar的隐藏/显示.

  1. func changeTabBar(hidden:Bool,animated: Bool){
  2. var tabBar = self.tabBarController?.tabBar
  3. if tabBar!.hidden == hidden{ return }
  4. let frame = tabBar?.frame
  5. let offset = (hidden ? (frame?.size.height)! : -(frame?.size.height)!)
  6. let duration:NSTimeInterval = (animated ? 0.5 : 0.0)
  7. tabBar?.hidden = false
  8. if frame != nil
  9. {
  10. UIView.animateWithDuration(duration,animations: {tabBar!.frame = CGRectOffset(frame!,offset)},completion: {
  11. println($0)
  12. if $0 {tabBar?.hidden = hidden}
  13. })
  14. }
  15. }

更新Swift 4

  1. func changeTabBar(hidden:Bool,animated: Bool){
  2. guard let tabBar = self.tabBarController?.tabBar else { return; }
  3. if tabBar.isHidden == hidden{ return }
  4. let frame = tabBar.frame
  5. let offset = hidden ? frame.size.height : -frame.size.height
  6. let duration:TimeInterval = (animated ? 0.5 : 0.0)
  7. tabBar.isHidden = false
  8.  
  9. UIView.animate(withDuration: duration,animations: {
  10. tabBar.frame = frame.offsetBy(dx: 0,dy: offset)
  11. },completion: { (true) in
  12. tabBar.isHidden = hidden
  13. })
  14. }

猜你在找的iOS相关文章