Skip to content

Instantly share code, notes, and snippets.

@MaciejGad
Last active June 11, 2018 18:42
Show Gist options
  • Save MaciejGad/9a4d1f65dcf382373911c90c548d2713 to your computer and use it in GitHub Desktop.
Save MaciejGad/9a4d1f65dcf382373911c90c548d2713 to your computer and use it in GitHub Desktop.
Hiding TabBar with animation
extension UITabBarController {
func set(visible: Bool, animated: Bool, completion: ((Bool)->Void)? = nil ) {
guard isVisible() != visible else {
completion?(true)
return
}
let offsetY = tabBar.frame.size.height
let duration = (animated ? 0.3 : 0.0)
let beginTransform:CGAffineTransform
let endTransform:CGAffineTransform
if visible {
beginTransform = CGAffineTransform(translationX: 0, y: offsetY)
endTransform = CGAffineTransform.identity
} else {
beginTransform = CGAffineTransform.identity
endTransform = CGAffineTransform(translationX: 0, y: offsetY)
}
tabBar.transform = beginTransform
if visible {
tabBar.isHidden = false
}
UIView.animate(withDuration: duration, animations: {
self.tabBar.transform = endTransform
}, completion: { compete in
completion?(compete)
if !visible {
self.tabBar.isHidden = true
}
})
}
func isVisible() -> Bool {
return !tabBar.isHidden
}
}
@MaciejGad
Copy link
Author

Based on HixField's and Bil Chan's answer I've prepared a version that works on iOS 9 to iOS 11 (I can't test on other versions right now), and iPhone 4S to iPhone X (tested). When you want to hide tab bar just call: tabBarController?.set(visible:false, animated: true), similarly if you want to show call: tabBarController?.set(visible:true, animated: true).

@hammadzz
Copy link

This does not work as expected. Leaves a gap at the bottom where the TabBar belongs. Tried it in a view with a NavigationBar + TabBar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment