我有一个自定义标签栏,我在中间添加一个按钮:
class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
setupMiddleButton()
}
func setupMiddleButton() {
let numberOfItems = CGFloat(tabBar.items!.count)
let tabBarItemSize = CGSize(width: tabBar.frame.width / numberOfItems, height: tabBar.frame.height)
menuButton.frame = CGRect(x: 0, y: 0, width: tabBarItemSize.width, height: tabBar.frame.size.height)
var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = self.view.bounds.height - menuButtonFrame.height - self.view.safeAreaInsets.bottom
menuButtonFrame.origin.x = self.view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame
menuButton.backgroundColor = UIColor.clear
menuButton.addTarget(self, action: #selector(menuButtonAction), for: UIControlEvents.touchUpInside)
self.view.addSubview(menuButton)
self.view.layoutIfNeeded()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
menuButton.frame.origin.y = self.view.bounds.height - menuButton.frame.height - self.view.safeAreaInsets.bottom
}
}
此栏显示在多个控制器中。
但是我有一个特定的控制器,我希望隐藏标签栏。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.isHidden = false
}
此代码工作正常,条实际上是隐藏的。
但是,如果我在中间点击(添加了menuButton),则会调用按钮操作(执行segue)。
隐藏标签栏时如何禁用按钮?
谢谢你的帮助! --------------更新解决方案 我不确定这是最好的解决方案,因为我是swift的新手,但似乎有效...... 在我的CustomTabBarController中,我添加了函数:
func hideTabBar() {
self.tabBar.isHidden = true
self.menuButton.isHidden = true
}
func showTabBar() {
self.tabBar.isHidden = false
self.menuButton.isHidden = false
}
每当我需要隐藏/显示它时,我称之为此功能。
在我的控制器中,我想隐藏它,我会这样做:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let tabBar = self.tabBarController as! FishBookTabBarController
tabBar.hideTabBar()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
let tabBar = self.tabBarController as! FishBookTabBarController
tabBar.showTabBar()
}
答案 0 :(得分:1)
您正在将按钮添加到self.view
,因此它不是""的一部分。标签栏。
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = true
self.menuButton.isHidden = true
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
self.tabBarController?.tabBar.isHidden = false
self.menuButton.isHidden = false
}
应该这样做。