Skip to content

Instantly share code, notes, and snippets.

@tesddev
Last active May 19, 2023 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tesddev/f4df52d2361823dcc8f31042cf8fea0f to your computer and use it in GitHub Desktop.
Save tesddev/f4df52d2361823dcc8f31042cf8fea0f to your computer and use it in GitHub Desktop.
Tab bar extension for hiding and showing tab bars in certain view controllers
/// Extension written by: @BrentMifsud, usage and slight modification by @tesddev
extension UITabBarController {
/// Extends the size of the `UITabBarController` view frame, pushing the tab bar controller off screen.
/// - Parameters:
/// - hidden: Hide or Show the `UITabBar`
/// - animated: Animate the change
func setTabBarHidden(_ hidden: Bool, animated: Bool) {
guard let vc = selectedViewController else { return }
guard tabBarHidden != hidden else { return }
let frame = self.tabBar.frame
let height = frame.size.height
let offsetY = hidden ? height : -height
UIViewPropertyAnimator(duration: animated ? 0.2 : 0, curve: .easeOut) {
self.tabBar.frame = self.tabBar.frame.offsetBy(dx: 0, dy: offsetY)
self.selectedViewController?.view.frame = CGRect(
x: 0,
y: 0,
width: vc.view.frame.width,
height: vc.view.frame.height + offsetY
)
self.view.setNeedsDisplay()
self.view.layoutIfNeeded()
}
.startAnimation()
}
/// Is the tab bar currently off the screen.
private var tabBarHidden: Bool {
tabBar.frame.origin.y >= UIScreen.main.bounds.height
}
}
/// TO USE
self.tabBarController?.setTabBarHidden(true, animated: true)
/// It is advisable to put in the ViewWillAppear of view controllers in which the tab bars always show.
/// This will return the tab bar in case you pop the view controller.
override func viewWillAppear(_ animated: Bool) {
self.tabBarController?.setTabBarHidden(false, animated: false)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment