Swift UITabBarController hide with animation
Solution 1
You could change the tab bar's frame inside an animation, so something like:
func hideTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height + (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}
func showTabBar() {
var frame = self.tabBarController?.tabBar.frame
frame?.origin.y = self.view.frame.size.height - (frame?.size.height)!
UIView.animate(withDuration: 0.5, animations: {
self.tabBarController?.tabBar.frame = frame!
})
}
Which sets the tab bar just below the visible screen, so that it slides up/down from the bottom.
Solution 2
I've developed a util extension for UIViewController
Swift 4 compatible:
extension UIViewController {
func setTabBarHidden(_ hidden: Bool, animated: Bool = true, duration: TimeInterval = 0.3) {
if animated {
if let frame = self.tabBarController?.tabBar.frame {
let factor: CGFloat = hidden ? 1 : -1
let y = frame.origin.y + (frame.size.height * factor)
UIView.animate(withDuration: duration, animations: {
self.tabBarController?.tabBar.frame = CGRect(x: frame.origin.x, y: y, width: frame.width, height: frame.height)
})
return
}
}
self.tabBarController?.tabBar.isHidden = hidden
}
}
Solution 3
Improvement of the response of @Luca Davanzo. If the bar is already hidden, it will continue hiding it and moving it lower. Also get rid of the return, so the state of the tabbar.hidden changes when the animation happens. So I added a check:
extension UIViewController {
func setTabBarHidden(_ hidden: Bool, animated: Bool = true, duration: TimeInterval = 0.5) {
if self.tabBarController?.tabBar.isHidden != hidden{
if animated {
//Show the tabbar before the animation in case it has to appear
if (self.tabBarController?.tabBar.isHidden)!{
self.tabBarController?.tabBar.isHidden = hidden
}
if let frame = self.tabBarController?.tabBar.frame {
let factor: CGFloat = hidden ? 1 : -1
let y = frame.origin.y + (frame.size.height * factor)
UIView.animate(withDuration: duration, animations: {
self.tabBarController?.tabBar.frame = CGRect(x: frame.origin.x, y: y, width: frame.width, height: frame.height)
}) { (bool) in
//hide the tabbar after the animation in case ti has to be hidden
if (!(self.tabBarController?.tabBar.isHidden)!){
self.tabBarController?.tabBar.isHidden = hidden
}
}
}
}
}
}
}
Solution 4
In case if you need to toggle it from hide to visible and vice versa:
func toggleTabbar() {
guard var frame = tabBarController?.tabBar.frame else { return }
let hidden = frame.origin.y == view.frame.size.height
frame.origin.y = hidden ? view.frame.size.height - frame.size.height : view.frame.size.height
UIView.animate(withDuration: 0.3) {
self.tabBarController?.tabBar.frame = frame
}
}
Solution 5
Swift 4 solution:
tabBarController?.tabBar.isHidden = true
UIView.transition(with: tabBarController!.view, duration: 0.35, options: .transitionCrossDissolve, animations: nil)
SwiftER
Im Business owner and developer. I believe that everyone in the world should learn to code. By learning coding everyone will understand ways to solve problems in the world.
Updated on July 27, 2022Comments
-
SwiftER almost 2 years
I'm trying to add animation to my tabBarController when hidden. Im able to accomplish this effect with the
navigationBarController
by usingself.navigationController?.isNavigationBarHidden = true
. I'm able to hide the tabBar by usingself.tabBarController?.tabBar.isHidden = true
but i do not get the animation how can I do this thank you in advance. -
Oleg G. almost 7 yearsIt's not Swift 3 compliant. "hidden" doesn't exist anymore.
-
Federico Malagoni over 6 yearsfixed to swift 3
-
Ahmadreza almost 6 yearson iPhone X , when tab bar is hidden there is a gray background of tab bar there.
-
Dylan almost 4 years@Alfi same here, did you fixed it?
-
Ahmadreza almost 4 years@Mark Dylan B Mercado, Sure I fixed it but I don't remember how, Sorry I does't have access to that project right now to help you more.
-
RealNmae over 3 yearsNice solution. But when calling hidden = false when tab is not hidden, it makes jumpy animation
-
RealNmae over 3 yearsThis solution is not save if you can call hidden=true when it's already hidden. It will move the frame endlessly down.