TabBar隐藏在viewDidLoad中但不在手势功能中

时间:2016-04-12 20:57:01

标签: ios swift uitabbarcontroller uitabbar viewcontroller

我尝试在viewDidLoad()中调用tabBarController!.tabBar.hidden = true并隐藏TabBar。但是,我尝试设置轻击手势并在Tap上隐藏栏。父视图中包含ScrollView的子视图(与IBOutlet连接为myView

override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: Selector("handleTap:"))
        myView.addGestureRecognizer(tap)
}

func handleTap(sender: UITapGestureRecognizer? = nil) {
   print("A") // logs successfully
           if TabBarHidden == false {
               print("B") // logs successfully

               //I tried:
               tabBarController?.tabBar.hidden = true

               // I also tried
               tabBarController?.tabBar.alpha = 0
               tabBarController?.tabBar.frame.origin.x += 50
               hidesBottomBarWhenPushed = true


           } else {
               ...
               TabBarHidden = false
           }
    }
正如我所说,当我在viewDidLoad中调用它时,

hidden可以正常工作,但如果我调用tap手​​势功能则不行。可能是什么问题?我错过了什么?

1 个答案:

答案 0 :(得分:1)

这段代码对我来说非常有用:

class ViewController: UIViewController {
    var tabBarHidden: Bool = false {
        didSet {
            tabBarController?.tabBar.hidden = tabBarHidden
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGestureRecognized(_:)))
        view.addGestureRecognizer(tapGestureRecognizer)
    }

    func tapGestureRecognized(sender: UITapGestureRecognizer) {
        tabBarHidden = !tabBarHidden
    }
}