How to hide a custom button on a TabBarController in Swift 3?
I have one UITabBarController
with a custom button in the middle of a TabBar. But I get strange behavior if I install hidesBottomBarWhenPushed = true
.
I created UITabBarController
programmatically in Swift 3.
Here is my code for creating a custom middle button:
func setupMiddleButton() {
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 48, height: 48))
var menuButtonFrame = menuButton.frame
menuButtonFrame.origin.y = view.bounds.height - menuButtonFrame.height
menuButtonFrame.origin.x = view.bounds.width/2 - menuButtonFrame.size.width/2
menuButton.frame = menuButtonFrame
menuButton.layer.cornerRadius = menuButtonFrame.height/2
view.addSubview(menuButton)
menuButton.setImage(UIImage(named: "updatemoment"), for: .normal)
menuButton.addTarget(self, action: #selector(menuButtonAction), for: .touchUpInside)
view.layoutIfNeeded()
}
func menuButtonAction() {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "NewPostID") as! UINavigationController
self.present(vc, animated: true, completion: nil)
print("segue success")
}
How to fix it? I want the middle button to stay in BottomBar
.
Thanks in advance.
source to share
I managed to fix it:
Creating a menu button in a class:
let menuButton = UIButton(frame: CGRect(x: 0, y: 0, width: 64, height: 64))
Adding two functions to the same controller (TabBarController):
func hideTabBar() {
self.tabBar.isHidden = true
self.menuButton.isHidden = true
}
func showTabBar() {
self.tabBar.isHidden = false
self.menuButton.isHidden = false
}
And then, whenever you need to hide or show the tabBar, use:
let tabBar = self.tabBarController as! InitialViewController
tabBar.showTabBar()
I am currently using it in viewWillAppear and viewWillDisappear on some controllers.
source to share