在询问之前,我已经经历了许多Stacksoverflow问题。没有人能够解决我的问题。我使用
制作了一个自定义按钮class TabBarViewController: UITabBarController {
let button = UIButton.init(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.setImage(UIImage(named: "assetIcon"), for: .normal)
button.backgroundColor = .blue
button.layer.cornerRadius = 35
self.view.insertSubview(button, aboveSubview: self.tabBar)
self.view.layoutIfNeeded()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let distance = ((self.view.bounds.height)/100)*11
// safe place to set the frame of button manually
button.frame = CGRect.init(x: self.tabBar.center.x - 32, y: self.view.bounds.height - distance, width: 70, height: 70)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func hideTabButton() {
self.button.isHidden = true
}
func showTabButton() {
self.button.isHidden = false
}
}
但是,当我从另一个要隐藏它的视图中调用hideTabButton的功能时,该按钮仍会显示。我从
调用了该功能 override func viewDidAppear(_ animated: Bool) {
let vc = storyboard?.instantiateViewController(withIdentifier: "tabBarVC") as! TabBarViewController
vc.hideTabButton()
}
它没有隐藏按钮。有解决方案或指南可以提供帮助的人吗?
答案 0 :(得分:1)
您正试图在创建它之前将其隐藏,您只需实例化TabBarViewController
现在就应该将标志从当前屏幕传递到TabBarViewController
屏幕,然后在TabBarViewController
屏幕中传递
请执行以下操作。
class TabBarViewController: UITabBarController {
var hideButton:Bool = false
let button = UIButton.init(type: .custom)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
button.setImage(UIImage(named: "assetIcon"), for: .normal)
button.backgroundColor = .blue
button.layer.cornerRadius = 35
self.view.insertSubview(button, aboveSubview: self.tabBar)
self.view.layoutIfNeeded()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
let distance = ((self.view.bounds.height)/100)*11
// safe place to set the frame of button manually
button.frame = CGRect.init(x: self.tabBar.center.x - 32, y: self.view.bounds.height - distance, width: 70, height: 70)
button.isHidden = hideButton
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func hideTabButton() {
self.button.isHidden = true
}
func showTabButton() {
self.button.isHidden = false
}
}
如果尚未实例化,现在只需将布尔值传递给TabbarViewController
override func viewDidAppear(_ animated: Bool) {
let vc = storyboard?.instantiateViewController(withIdentifier: "tabBarVC") as! TabBarViewController
vc.hideButton=true
}
如果您的标签栏已被实例化,请再次对其进行实例化,如下所示
if let tabController = self.tabbarController as? TabbarViewController{ tabController.button.isHidden = true }
当尝试取消隐藏时,只需将true更改为false
if let tabController = self.tabbarController as? TabbarViewController{ tabController.button.isHidden = false }