Search code examples
iosxcodeswiftios9

How to hide on swipe programmatically in Swift


How can I implement hide on swipe programmatically? the reason why I don't want to use the Hide Bars on swipe option from the attributes menu is because I came across a problem. When enabled, the app will hide the navigation bar, as well as the toolbar if the user scrolls down the content view, and the bars will appear when the user swipes back up. I don't want this change to apply to all the navigation bars of the entire app. I want to be able to just hide one navigation bar. I did gave this an attempt, I set the hidesBarsOnSwipe property to true as shown in the code below navigationController?.hideBarsOnSwipe = true but I came across other problems, I tried placing my code above the viewDidLoad method in the ViewControllers where this feature will be enabled and I set the property to false on the ones I don't want to this featured to be enabled.

My problem: I can't hide the navigation bar after navigating back to the view controller with the false property

I don't understand what I'm doing wrong, any suggestions?


Solution

  • Instead of putting your code in viewDidLoad(), put it in viewWillAppear() -- that ensures it's called every time your View Controller comes on screen, since with Navigation Controllers, the viewDidLoad() method isn't always called when view controllers are popped. Also make sure to explicitly mention the preffered setting for each view controller -- i.e. navigationController?.hideBarsOnSwipe in the viewWillAppear() of each of your View Controllers.

    Hope this helps.