0

I have 3 bar items in my tabbar, the middle one I set disable and overlay it with my middle custom button like this

Swift 3 - How do I create a prominent button on a tab bar (e.g. camera button)

I want to hide bottom bar when navigation pushed a new screen. I try this but it does not work because viewDidDisappear was never called

override func viewDidDisappear(_ animated: Bool) {
    super.viewDidAppear(animated)
    hidesBottomBarWhenPushed = true
    centerButton.isHidden = true
}

Coud you guys show me how to achieve that?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
coinhndp
  • 2,281
  • 9
  • 33
  • 64

2 Answers2

0

Looks like you have a typo there. Use super.viewDidDisappear()

override func viewDidDisappear(_ animated: Bool) {
    //super.viewDidAppear(animated)
    super.viewDidDisappear(animated)
    hidesBottomBarWhenPushed = true
    centerButton.isHidden = true
}
Rikesh Subedi
  • 1,755
  • 22
  • 21
0

You have to set that property for the view controller that is pushed. It is important to notice that setting it in viewDidLoad is too late. You should set in when the view controller instance is initialized instead:

class PushedVC: UIViewController {

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        sharedInit()
    }

    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
        sharedInit()
    }

    private func sharedInit() {
        hidesBottomBarWhenPushed = true
    }

}

Of course you could also just check the checkmark Hide Bottom Bar on Push for the specific view controller when using storyboards.

To make sure that your center button gets hidden / shown with the tab bar in this approach you have to add it as a subview of the tab bar in your UITabBar subclass. Then you override hitTest to make it fully tappable (of course that is only needed if the button exceeds the tab bar's frame):

class TabBar: UITabBar {

    let centerButton = ...

    // ...

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if centerButton.frame.contains(point) {
            return centerButton
        }

        return super.hitTest(point, with: event)
    }

    // ...

}
André Slotta
  • 13,774
  • 2
  • 22
  • 34
  • Button my custom button is not part of tabbar controller. And if I add it to tabbarcontroller view, I cannot click on the upper part of the button because it is out of the tabbar view. Note that my button is bigger than the tabbar (half of it is in the tabbar and the other half is not) –  coinhndp Dec 28 '18 at 10:55
  • Where do you add the button instead? And what do you want to hide? Only the button? Or the whole tab bar including the button? – André Slotta Dec 28 '18 at 11:56
  • The whole bar include the button. When navigation controller push a new screen, my tabbar and also my custom bottom should be hidden. My button looks like this https://stackoverflow.com/questions/42477440/swift-3-how-do-i-create-a-prominent-button-on-a-tab-bar-e-g-camera-button –  coinhndp Dec 28 '18 at 12:01
  • So with my solution the bar gets hidden correctly but the button stays in place? Do I get that correctly? – André Slotta Dec 28 '18 at 12:07
  • True. And if I add the button to tabbar (tabbar.addSubview(button)). Then it works but my button is higher than the tabbar, and we can only click on part of the button (within the tabbar view). Any idea rather than using hit test? –  coinhndp Dec 28 '18 at 12:15
  • In what class do you add the button right now? Not in the tab bar controller subclass but? – André Slotta Dec 28 '18 at 17:25
  • And btw... What's wrong with the `hitTest` approach? – André Slotta Dec 28 '18 at 17:42
  • hitTest seems too much. If I cannot find any answers than hitTest is definitely my choice. I add the button to the controller that hosts tabbar (CustomTabbarController.swift) –  coinhndp Dec 28 '18 at 19:16
  • @coinhndp Updated my answer. – André Slotta Dec 29 '18 at 12:29