Last active
April 28, 2023 23:35
-
-
Save BrentMifsud/a967020dceaf4ebe28a448a049e7f67c to your computer and use it in GitHub Desktop.
Show/Hide UITabBar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
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.3 : 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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment