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 }
allocate Apr 16 '15 at 19:09 2015-04-16 19:09
source share