Created
February 3, 2018 10:59
-
-
Save brindy/b93d0cd0ce4ca3560225ade23f4cf35e to your computer and use it in GitHub Desktop.
UITabBarController extension for hiding and showing TabBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Also works well on iPhone X | |
// Example usage: | |
/* | |
override func viewWillAppear(_ animated: Bool) { | |
super.viewWillAppear(animated) | |
tabBarController?.setTabBarVisible(visible: false, animated: true) | |
} | |
*/ | |
extension UITabBarController { | |
// Inspired by https://gist.github.com/krodak/2bd8139c5f69b9434008 | |
func setTabBarVisible(visible:Bool, animated:Bool) { | |
// bail if the current state matches the desired state | |
let isVisible = tabBar.frame.origin.y < UIScreen.main.bounds.height | |
guard (isVisible != visible) else { return } | |
// get a frame calculation ready | |
let frame = tabBar.frame | |
let height = frame.size.height | |
let offsetY = (visible ? -height : height) | |
// zero duration means no animation | |
let duration:TimeInterval = (animated ? 0.3 : 0.0) | |
// animate the tabBar | |
UIView.animate(withDuration: duration) { | |
self.tabBar.frame = frame.offsetBy(dx: 0, dy: offsetY) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment