Marco
Marco

Reputation: 1061

Swift Tab bar hidden but custom button still active

I have a custom tab Bar where i add a button in the middle:

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
 }
}

This bar is shown in multiple controller.

However i have a specific controller where i'd like the tab bar to be hidden.

 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
}

This code works fine and the bar is actually hidden.

However If i click in the middle (where the menuButton is added) the button action is called (a segue is performed).

How can I disable the button when hiding the Tab bar?

Thank you for the help! --------------UPDATE Solution I am not sure this is the best solution because i'm new to swift, but it seems to work... in my CustomTabBarController I have added to function:

func hideTabBar() {
    self.tabBar.isHidden = true
    self.menuButton.isHidden = true
}

func showTabBar() {
    self.tabBar.isHidden = false
    self.menuButton.isHidden = false
}

the whenever i need to hide/display it i call this functions.

In my case in the controller where i'd like to hide it i do so:

  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()
}

Upvotes: 2

Views: 1608

Answers (1)

DonMag
DonMag

Reputation: 77423

You are adding your button to self.view, so it is not "part of" your tab bar.

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
}

That should do it.

Upvotes: 1

Related Questions