Reputation: 1009
I've got a tab bar like this:
The middle button is just a UIImage that is added in a sub-class of the tabBarController like so:
class TabBarDelegate: UITabBarController {
let button = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
self.tabBar.unselectedItemTintColor = #colorLiteral(red: 0.6666666865, green: 0.6666666865, blue: 0.6666666865, alpha: 1)
button.backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
button.image = UIImage(named: "tabBarAdd")
button.setImageColor(color: #colorLiteral(red: 0.184496969, green: 0.6701939702, blue: 0.3898918033, alpha: 1))
let imageView = UIImageView()
imageView.image = UIImage(named: "tabBarAdd")
imageView.bounds = CGRect(x: button.frame.origin.x, y: button.frame.origin.y, width: button.frame.width, height: button.frame.height)
self.view.insertSubview(button, aboveSubview: self.tabBar)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
button.frame = CGRect.init(x: self.tabBar.center.x - 17.5, y: self.view.bounds.height - 80, width: 35, height: 35)
button.layer.cornerRadius = 17.5
}
}
My image isn't currently the correct color so I'm using an extention to change the color, which is the button.setImageColor(color: _)
function in the code.
The middle button is working fine when I press it, the touch just goes through the image and to the tabBarItem.
What I would like to do is when there's a transition to the middle view controller, to hide the image.
I've tried using the viewDidAppear()
function in the viewController that's appearing. I used this code:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let tabBarD = TabBarDelegate()
tabBarD.button.isHidden = true
}
but that's not working. I've also tried removing the image from the superview entirely using tabBarD.button.removeFromSuperview()
but even that doesn't work.
I'd love any ideas and help.
Thanks in advance :)
Upvotes: 0
Views: 208
Reputation: 1009
I figured it out!
Thank you to emrepun who got me started thinking in a different way on how to solve it!
I ended up using an extension to the delegate method like so:
extension TabBarDelegate: UITabBarControllerDelegate {
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController.hidesBottomBarWhenPushed {
self.button.isHidden = true
}
return true
}
}
And since I was using the hidesBottomBarWhenPushed
method from the storyboard this worked!
Thanks for your time
Upvotes: 1
Reputation: 2666
In your viewDidAppear
method of middleViewController
, you are actually initializing a new TabBarController
, thats why it does not work in the way you intend.
Delete that code, and add two methods to your base ViewControllers
in tabBarController
, viewWillDisAppear
, and viewWillAppear
:
func viewWillDisappear(_ animated: Bool) {
super.viewWilDisappear(animated)
if let tabBarController = self.tabBarController as? TabBarDelegate {
tabBarController.button.isHidden = true
}
}
func viewWillAppear(_ animated: Bool) {
super.viewWilAppear(animated)
if let tabBarController = self.tabBarController as? TabBarDelegate {
tabBarController.button.isHidden = false
}
}
So each time you will go to another viewController (presumably detail viewController, from the base controllers of a tabBarController, you hide it when one of the baseViewController's view is going to disappear, then you make it visible again if the view is going to appear again.
Upvotes: 0