Extension on UITabBarController for hiding/showing the tab bar.
extension UITabBarController { | |
/** | |
Show or hide the tab bar. | |
- Parameter hidden: `true` if the bar should be hidden. | |
- Parameter animated: `true` if the action should be animated. | |
- Parameter transitionCoordinator: An optional `UIViewControllerTransitionCoordinator` to perform the animation | |
along side with. For example during a push on a `UINavigationController`. | |
*/ | |
func setTabBar( | |
hidden: Bool, | |
animated: Bool = true, | |
along transitionCoordinator: UIViewControllerTransitionCoordinator? = nil | |
) { | |
guard isTabBarHidden != hidden else { return } | |
let offsetY = hidden ? tabBar.frame.height : -tabBar.frame.height | |
let endFrame = tabBar.frame.offsetBy(dx: 0, dy: offsetY) | |
let vc: UIViewController? = viewControllers?[selectedIndex] | |
var newInsets: UIEdgeInsets? = vc?.additionalSafeAreaInsets | |
let originalInsets = newInsets | |
newInsets?.bottom -= offsetY | |
/// Helper method for updating child view controller's safe area insets. | |
func set(childViewController cvc: UIViewController?, additionalSafeArea: UIEdgeInsets) { | |
cvc?.additionalSafeAreaInsets = additionalSafeArea | |
cvc?.view.setNeedsLayout() | |
} | |
// Update safe area insets for the current view controller before the animation takes place when hiding the bar. | |
if hidden, let insets = newInsets { set(childViewController: vc, additionalSafeArea: insets) } | |
guard animated else { | |
tabBar.frame = endFrame | |
return | |
} | |
// Perform animation with coordinato if one is given. Update safe area insets _after_ the animation is complete, | |
// if we're showing the tab bar. | |
weak var tabBarRef = self.tabBar | |
if let tc = transitionCoordinator { | |
tc.animateAlongsideTransition(in: self.view, animation: { _ in tabBarRef?.frame = endFrame }) { context in | |
if !hidden, let insets = context.isCancelled ? originalInsets : newInsets { | |
set(childViewController: vc, additionalSafeArea: insets) | |
} | |
} | |
} else { | |
UIView.animate(withDuration: 0.3, animations: { tabBarRef?.frame = endFrame }) { didFinish in | |
if !hidden, didFinish, let insets = newInsets { | |
set(childViewController: vc, additionalSafeArea: insets) | |
} | |
} | |
} | |
} | |
/// `true` if the tab bar is currently hidden. | |
var isTabBarHidden: Bool { | |
return !tabBar.frame.intersects(view.frame) | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
extension UITabBarController {
} Refactored |
This comment has been minimized.
This comment has been minimized.
Sweet! I'm not using this myself anymore, but maybe others have use for it :) |
This comment has been minimized.
This comment has been minimized.
that only hide tabbar not tabbar item |
This comment has been minimized.
This comment has been minimized.
I have recently used this extension. I found out one thing that after hiding the tabbar and then sending the app to background. Then again opening the app the tabbar reappears. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
This does not work properly on iPhone X...