How to hide tab bar with animation in iOS?

后端 未结 15 1805
夕颜
夕颜 2020-11-30 19:20

So I have a button that is connected to a IBAction. When I press the button I want to hide the tab bar in my iOS app with a animation. This [self setTabBarHidden:hidde

相关标签:
15条回答
  • 2020-11-30 20:23

    Rewrite Sherwin Zadeh's answer in Swift 4:

    /* tab bar hide/show animation */
    extension AlbumViewController {
        // pass a param to describe the state change, an animated flag and a completion block matching UIView animations completion
        func setTabBarVisible(visible: Bool, animated: Bool, completion: ((Bool)->Void)? = nil ) {
    
            // bail if the current state matches the desired state
            if (tabBarIsVisible() == visible) {
                if let completion = completion {
                    return completion(true)
                }
                else {
                    return
                }
            }
    
            // get a frame calculation ready
            let height = tabBarController!.tabBar.frame.size.height
            let offsetY = (visible ? -height : height)
    
            // zero duration means no animation
            let duration = (animated ? kFullScreenAnimationTime : 0.0)
    
            UIView.animate(withDuration: duration, animations: {
                let frame = self.tabBarController!.tabBar.frame
                self.tabBarController!.tabBar.frame = frame.offsetBy(dx: 0, dy: offsetY)
            }, completion:completion)
        }
    
        func tabBarIsVisible() -> Bool {
            return tabBarController!.tabBar.frame.origin.y < view.frame.maxY
        }
    }
    
    0 讨论(0)
  • 2020-11-30 20:24

    Try to set the frame of the tabBar in animation. See this tutorial.

    Just be aware, it's bad practice to do that, you should set show/hide tabBar when UIViewController push by set the property hidesBottomBarWhenPushed to YES.

    0 讨论(0)
  • 2020-11-30 20:24

    Unfortunately, I can't comment on HixField's answer because I don't have enough reputation, so I have to leave this as a separate answer.

    His answer is missing the computed property for movedFrameView, which is:

    var movedFrameView:CGRect? {
      get { return objc_getAssociatedObject(self, &AssociatedKeys.movedFrameView) as? CGRect }
      set { objc_setAssociatedObject(self, &AssociatedKeys.movedFrameView, newValue, .OBJC_ASSOCIATION_COPY) }
    }
    
    0 讨论(0)
提交回复
热议问题