1

I have the hidesBottomBarWhenPushed = true set for one of my UIViewController's (call it ViewControllerA) that is pushed onto my UINavigationController stack. I also opt to show the bottomBar when I push a new ViewController ontop of ViewControllerA. Therefore I have:

class ViewControllerA: UIViewController {

override func viewWillDisappear(animated: Bool) {
    self.hidesBottomBarWhenPushed = false
}

override func viewWillAppear(animated: Bool) {
    self.hidesBottomBarWhenPushed = true
}

This all works fine.

When I push ViewControllerA, the bottom bar hides. When I push any other ViewController, the bottom bar shows.

However, when I am traveling backwards in the navigation stack (aka hitting the UIBarButtonItemBack button), I cannot get the bottomBar to hide when I pop the navigation stack to reveal ViewControllerA.

What am I missing? Thanks!

Alex
  • 5,298
  • 4
  • 29
  • 34
  • Possible duplicate of http://stackoverflow.com/questions/6117717/when-using-hidesbottombarwhenpushed-i-want-the-tab-bar-to-reappear-when-i-push – SwiftArchitect Jul 15 '15 at 02:56
  • This question is referring to hiding the tab bar when the second view controller is popped, not showing it again when the second view controller is pushed. – Mark Jul 15 '15 at 03:07
  • @Mark correct. Thank you. – Alex Jul 15 '15 at 03:11
  • Although in that post it does look like they mention taking the proper steps to show it when the user presses the back button. – Alex Jul 15 '15 at 03:17
  • try this answer http://stackoverflow.com/a/36148064/3078925 – Beslan Tularov Mar 22 '16 at 07:15

2 Answers2

1

Got it! Here's what worked:

class ViewControllerCustom: UIViewController {
  init() {
    self.hidesBottomBarWhenPushed = true
  }

  override func viewDidAppear(animated: Bool) {
    self.hidesBottomBarWhenPushed = false
  }
}

And then in every UIViewController's custom implementation of BarButtonItemBack pressed I check to see if the previous view controller (that will be popped to needs to hide the tab bar). Granted I abstracted this out into a general function so I didn't need to repeat code, but here's the concept. Thanks for the help figuring this out though!

func barButtonItemBackPressed(button: UIButton) {

  var viewControllers = self.navigationController!.viewControllers as! [UIViewController]
  if ((viewControllers[viewControllers.count - 2]).isKindOfClass(ViewControllerCustom.self)) {
    (viewControllers[viewControllers.count - 2] as! ViewControllerCustom).hidesBottomBarWhenPushed = true
  }

  self.navigationController?.popViewControllerAnimated(true)
}
Alex
  • 5,298
  • 4
  • 29
  • 34
0

I believe the intended use of this property is to hide the bar when pushed. So, when your view controller appears after the top-most one is popped, it wasn't pushed on the stack, so it doesn't change the tab bar's appearance.

This leaves you with two options:

1) Keep the bottom bar for all view controllers. When text is being entered, the keyboard covers the bottom bar.

2) Hide the bottom bar for View Controller A, as well as any other view controller that is pushed on top of A.

Mark
  • 7,167
  • 4
  • 44
  • 68
  • 1
    But if this property was there to hide the bottom bar for a particular view controller, wouldn't the expectation be it should be hidden when the view controller is presented again, aka popped to – Alex Jul 15 '15 at 03:12
  • I think the intended use is to hide the bar when pushing a view controller, assuming no other pushed view controllers will need the bar shown. It essentially guarantees that the user can't jump to a different tab without first popping to the root of the navigation controller. You're expecting the property to behave more like a navigation item, which each view controller uses to customize the navigation controller, regardless of the transition. What is your specific use case? – Mark Jul 15 '15 at 03:17
  • For example, take a comment view controller. We don't want the tab bar, since the keyboard and the input field are at the bottom. Many apps choose to hide the tab bar when they have a comment view. Yet from a comment view you can navigate forwards to say something like a user view. So now my issue is, when the user goes back to the comment view, the tab bar is shown and it is not correct. – Alex Jul 15 '15 at 03:20
  • Ah I see. I've updated my answer with the two solutions that are the safest. You may be able to get the behaviour you expect by hacking the tab bar, but I recommend one of my answers. Unfortunately it seems like you can't dynamically show and hide the tab bar on both pushes and pops inside a navigation controller. – Mark Jul 15 '15 at 03:33
  • I see. I'll play around with it for a bit, but appreciate the insight. Thanks! – Alex Jul 15 '15 at 03:34