Skip to content

Instantly share code, notes, and snippets.

@brindy
Created February 3, 2018 10:59
Show Gist options
  • Save brindy/b93d0cd0ce4ca3560225ade23f4cf35e to your computer and use it in GitHub Desktop.
Save brindy/b93d0cd0ce4ca3560225ade23f4cf35e to your computer and use it in GitHub Desktop.
UITabBarController extension for hiding and showing TabBar
// 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