我试图模仿UINavigationController的新的hidesBarsOnTap与选项卡.我已经看到很多答案,或者指向在一个viewController上设置hidesBottomBarWhenPushed,该控件只会完全隐藏它,而不是在点击时.
@IBAction func tapped(sender: AnyObject) { // what goes here to show/hide the tabBar ??? }
提前致谢
编辑:根据下面的建议,我试过
self.tabBarController?.tabBar.hidden = true
确实隐藏了tabBar(轻击切换true / false),但没有动画.我会问一个单独的问题.
解决方法
经过多次狩猎和尝试使用Swift优雅地隐藏/显示UITabBar的各种方法,我可以使用
this great solution by danh并将其转换为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:NSTimeInterval = (animated ? 0.3 : 0.0) // animate the tabBar if frame != nil { UIView.animateWithDuration(duration) { self.tabBarController?.tabBar.frame = CGRectOffset(frame!,offsetY!) return } } } func tabBarIsVisible() ->Bool { return self.tabBarController?.tabBar.frame.origin.y < CGRectGetMaxY(self.view.frame) } // Call the function from tap gesture recognizer added to your view (or button) @IBAction func tapped(sender: AnyObject) { setTabBarVisible(!tabBarIsVisible(),animated: true) }