How to hide / show tabBar when using with Swift in iOS8

I am trying to imitate the UINavigationController new hidesBarsOnTap using the tab bar. I saw many answers to this that indicate setting hidesBottomBarWhenPushed on the viewController, which only hides it completely, and not when clicked.

  @IBAction func tapped(sender: AnyObject) { // what goes here to show/hide the tabBar ??? } 

early

EDIT: as per suggestion below I tried

 self.tabBarController?.tabBar.hidden = true 

which really hides tabBar (toggles true / false when pressed), but without animation. I will ask that as a separate issue.

+25
ios ios8 swift uitabbarcontroller
Nov 19 '14 at 4:02
source share
9 answers

After much searching and trying out various methods to gracefully hide / show UITabBar using Swift, I was able to take this great solution from danh and convert it to Swift:

 func setTabBarVisible(visible: Bool, animated: Bool) { //* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { return } // get a frame calculation ready let frame = self.tabBarController?.tabBar.frame let height = frame?.size.height let offsetY = (visible ? -height! : height) // zero duration means no animation let duration: TimeInterval = (animated ? 0.3 : 0.0) // animate the tabBar if frame != nil { UIView.animate(withDuration: duration) { self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!) return } } } func tabBarIsVisible() -> Bool { return (self.tabBarController?.tabBar.frame.origin.y)! < self.view.frame.maxY } // Call the function from tap gesture recognizer added to your view (or button) @IBAction func tapped(_ sender: Any?) { setTabBarVisible(visible: !tabBarIsVisible(), animated: true) } 
+51
Nov 22 '14 at 0:59
source share
β€” -

Favorite Michael Campsall answered. Here is the same code as the extension, if anyone is interested:

Swift 2.3

 extension UITabBarController { func setTabBarVisible(visible:Bool, animated:Bool) { // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { return } // get a frame calculation ready let frame = self.tabBar.frame let height = frame.size.height let offsetY = (visible ? -height : height) // animate the tabBar UIView.animateWithDuration(animated ? 0.3 : 0.0) { self.tabBar.frame = CGRectOffset(frame, 0, offsetY) self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY) self.view.setNeedsDisplay() self.view.layoutIfNeeded() } } func tabBarIsVisible() ->Bool { return self.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame) } } 

Swift 3

 extension UIViewController { func setTabBarVisible(visible: Bool, animated: Bool) { //* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time // bail if the current state matches the desired state if (isTabBarVisible == visible) { return } // get a frame calculation ready let frame = self.tabBarController?.tabBar.frame let height = frame?.size.height let offsetY = (visible ? -height! : height) // zero duration means no animation let duration: TimeInterval = (animated ? 0.3 : 0.0) // animate the tabBar if frame != nil { UIView.animate(withDuration: duration) { self.tabBarController?.tabBar.frame = frame!.offsetBy(dx: 0, dy: offsetY!) return } } } var isTabBarVisible: Bool { return (self.tabBarController?.tabBar.frame.origin.y ?? 0) < self.view.frame.maxY } } 
+27
Jul 23 '15 at 19:03
source share

I had to adapt the accepted answer to this question a little. He hid the bar, but my eyes did not fit properly, so I had a space below.

The following code successfully animates the hiding of the tab bar when resizing the view to avoid this problem.

Updated for Swift 3 (now with less ugly code)

 func setTabBarVisible(visible: Bool, animated: Bool) { guard let frame = self.tabBarController?.tabBar.frame else { return } let height = frame.size.height let offsetY = (visible ? -height : height) let duration: TimeInterval = (animated ? 0.3 : 0.0) UIView.animate(withDuration: duration, delay: 0.0, options: UIViewAnimationOptions.curveEaseIn, animations: { [weak self] () -> Void in guard let weakSelf = self else { return } weakSelf.tabBarController?.tabBar.frame = frame.offsetBy(dx: 0, dy: offsetY) weakSelf.view.frame = CGRect(x: 0, y: 0, width: weakSelf.view.frame.width, height: weakSelf.view.frame.height + offsetY) weakSelf.view.setNeedsDisplay() weakSelf.view.layoutIfNeeded() }) } func handleTap(recognizer: UITapGestureRecognizer) { setTabBarVisible(visible: !tabBarIsVisible(), animated: true) } func tabBarIsVisible() -> Bool { guard let tabBar = tabBarController?.tabBar else { return false } return tabBar.frame.origin.y < UIScreen.main.bounds.height } 

Old version of Swift 2

 func setTabBarVisible(visible: Bool, animated: Bool) { // hide tab bar let frame = self.tabBarController?.tabBar.frame let height = frame?.size.height var offsetY = (visible ? -height! : height) println ("offsetY = \(offsetY)") // zero duration means no animation let duration:NSTimeInterval = (animated ? 0.3 : 0.0) // animate tabBar if frame != nil { UIView.animateWithDuration(duration) { self.tabBarController?.tabBar.frame = CGRectOffset(frame!, 0, offsetY!) self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY!) self.view.setNeedsDisplay() self.view.layoutIfNeeded() return } } } @IBAction func handleTap(recognizer: UITapGestureRecognizer) { setTabBarVisible(!tabBarIsVisible(), animated: true) } func tabBarIsVisible() -> Bool { return self.tabBarController?.tabBar.frame.origin.y < UIScreen.mainScreen().bounds.height } 
+13
Apr 16 '15 at 19:09
source share

You can simply add this line to ViewDidLoad () in swift:

 self.tabBarController?.tabBar.hidden = true 
+7
Jul 01 '15 at 9:22
source share

I use tabBar.hidden = YES in ObjC to hide the tab bar in certain cases. However, I did not try to connect it to the event with a tap.

+2
Nov 19 '14 at 7:38
source share

The code is fine, but when you use presentViewController , tabBarIsVisible() does not work. To keep the UITabBarController always covertly using only this part:

 extension UITabBarController { func setTabBarVisible(visible:Bool, animated:Bool) { let frame = self.tabBar.frame let height = frame.size.height let offsetY = (visible ? -height : height) UIView.animateWithDuration(animated ? 0.3 : 0.0) { self.tabBar.frame = CGRectOffset(frame, 0, offsetY) self.view.frame = CGRectMake(0, 0, self.view.frame.width, self.view.frame.height + offsetY) self.view.setNeedsDisplay() self.view.layoutIfNeeded() } } } 
+2
Aug 21 '15 at 16:40
source share

Swift 3 version:

 func setTabBarVisible(visible:Bool, animated:Bool) { //* This cannot be called before viewDidLayoutSubviews(), because the frame is not set before this time // bail if the current state matches the desired state if (tabBarIsVisible() == visible) { return } // get a frame calculation ready let frame = self.tabBarController?.tabBar.frame let height = frame?.size.height let offsetY = (visible ? -height! : height) // zero duration means no animation let duration:TimeInterval = (animated ? 0.3 : 0.0) // animate the tabBar if frame != nil { UIView.animate(withDuration: duration) { self.tabBarController?.tabBar.frame = (self.tabBarController?.tabBar.frame.offsetBy(dx: 0, dy: offsetY!))! return } } } func tabBarIsVisible() ->Bool { return (self.tabBarController?.tabBar.frame.origin.y)! < self.view.frame.midY } 
+1
Mar 08 '18 at 13:46
source share

For Swift 4 and animation + hiding, putting the tabBar outside the view :

 if let tabBar = tabBarController?.tabBar, let y = tabBar.frame.origin.y + tabBar.frame.height { UIView.animate(withDuration: 0.2) { tabBar.frame = CGRect(origin: CGPoint(x: tabBar.frame.origin.x, y: y), size: tabBar.frame.size) } } 
0
Nov 07 '18 at 17:52
source share

To make the animation work with self.tabBarController?.tabBar.hidden = true , just do the following:

 UIView.animateWithDuration(0.2, animations: { self.tabBarController?.tabBar.hidden = true }) 

Besides another solution, this will also work well with autorun.

-3
Sep 26 '15 at 15:36
source share



All Articles