How to hide tab bar with animation in iOS?

后端 未结 15 1927
夕颜
夕颜 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
        }
    }
    

提交回复
热议问题