我正在尝试使用标签栏模仿 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: 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)
}
关于ios - 在 iOS8 中使用 Swift 点击时如何隐藏/显示 tabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27008737/