2

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx for the answer.... the tab bar controller (and so the button) is a separate controller so i cannot call the menuButton in the controller where i need to hide it...i tried to instantiate the CustomTabBar but i was getting a found nil on optional value... i think i might have found a solution... i'll update my question..
Just realised you've suggested the same thing... :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.