How to hide tab bar with animation in iOS?

后端 未结 15 1896
夕颜
夕颜 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:19

    [Swift4.2]

    Just created an extension for UITabBarController:

    import UIKit
    
    extension UITabBarController {
        func setTabBarHidden(_ isHidden: Bool, animated: Bool, completion: (() -> Void)? = nil ) {
            if (tabBar.isHidden == isHidden) {
                completion?()
            }
    
            if !isHidden {
                tabBar.isHidden = false
            }
    
            let height = tabBar.frame.size.height
            let offsetY = view.frame.height - (isHidden ? 0 : height)
            let duration = (animated ? 0.25 : 0.0)
    
            let frame = CGRect(origin: CGPoint(x: tabBar.frame.minX, y: offsetY), size: tabBar.frame.size)
            UIView.animate(withDuration: duration, animations: {
                self.tabBar.frame = frame
            }) { _ in
                self.tabBar.isHidden = isHidden
                completion?()
            }
        }
    }
    
    

提交回复
热议问题