override func viewDidLoad() {
let tap = UITapGestureRecognizer(target: self, action: #selector(touchHandled))
view.addGestureRecognizer(tap)
}
@objc func touchHandled() {
tabBarController?.hideTabBarAnimated(hide: true)
}
extension UITabBarController {
func hideTabBarAnimated(hide:Bool) {
UIView.animate(withDuration: 2, animations: {
if hide {
self.tabBar.transform = CGAffineTransform(translationX: 0, y: 100)
} else {
self.tabBar.transform = CGAffineTransform(translationX: 0, y: -100)
}
})
}
}
我只能隐藏标签栏,但当你再次点击时我无法让它显示。我试图寻找堆栈溢出的答案,但答案似乎只有在您使用按钮或 Storyboard 时才有效。
在类中有一个变量 isTabBarHidden
,用于存储 tabBar 是否已被动画隐藏。 (您可以使用 tabBar.isHidden,但是在动画隐藏和显示时会使逻辑复杂一点)
class ViewController {
var isTabBarHidden = false // set the default value as required
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(touchHandled))
view.addGestureRecognizer(tap)
}
@objc func touchHandled() {
guard let tabBarControllerFound = tabBarController else {
return
}
tabBarController?.hideTabBarAnimated(hide: !isTabBarHidden)
isTabBarHidden = !isTabBarHidden
}
}
关于ios - 点击部分 View 时如何真正隐藏和显示标签栏? (没有按钮,但屏幕的任何位置),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52014063/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |