Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/hide navigationBar when view is pushed/popped in iOS 8

Tags:

Hi fellow developers,

I'm suprised that I didn't find any information on that particular use case for iOS 8. Let me explain, I have a navigation controller stack, the first view has it's navigationBar hidden, when the user clicks on a cell a new view is pushed and I need the navigation bar shown on this view to go back (obviously). On iOS 7 it was just a matter of adding this line in viewWillAppear :

 [self.navigationController setNavigationBarHidden:NO animated:YES]; 

And it works like a charm. But as of iOS 8 I'm struggling to have the same behavior. For now I managed to do the same by using :

- (void)viewDidLayoutSubviews {     [super viewDidLayoutSubviews];     if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0"))         [self.navigationController setNavigationBarHidden:NO animated:NO]; } - (void)viewDidLoad {      [super viewDidLoad];      if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {          self.automaticallyAdjustsScrollViewInsets = NO;          self.edgesForExtendedLayout = UIRectEdgeNone;          self.collectionView.contentInset = UIEdgeInsetsMake(self.navigationController.navigationBar.frame.size.height, 0, 0, 0); }    } 

And changing the contentInset of my collectionsView / tableviews whatever I need to display. It'll work, but it's a real pain in the * (sometimes the content inset isn't necessary if the user comes from a new viewcontroller).

For context the project was first developed to handle iOS 6 and 7, so no storyboard, all is done programmatically. I ported the app to a more modern codebase and made it universal.

Am I missing something ? Is there a better way to handle that in iOS 7/8 ?

like image 825
nebuto Avatar asked Sep 16 '14 10:09

nebuto


People also ask

How to hide the navigation bar ios?

Go to the Attributes Inspector and in the Image View section set the image to sunset_small. The Storyboard should look like this. The hidesBarsOnTap boolean of the Navigation Controller is set to true to enable hiding the Navigation Bar on tap.

How to hide navigation bar in Uiviewcontroller?

To hide or show the navigation bar, use the isNavigationBarHidden property or setNavigationBarHidden(_:animated:) method.

How do I show hidden navigation bar?

Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How to hide navigation bar on specific view controller Swift?

How To Hide Navigation Bar In Swift. To hide the navigation bar in Swift, you'll need to add code to two methods: viewWillAppear and viewWillDisappear . That's it to hide the navigation bar in your view controller.


1 Answers

You want to hide it in viewWillAppear and show it again in viewWillDisappear

override func viewWillAppear(_ animated: Bool) {     super.viewWillAppear(animated)     navigationController?.navigationBar.isHidden = true }  override func viewWillDisappear(_ animated: Bool) {     super.viewWillDisappear(animated)     navigationController?.navigationBar.isHidden = false } 
like image 66
Nick Wargnier Avatar answered Sep 19 '22 18:09

Nick Wargnier