1

I have a UITableViewController inside a navigationController. I have code that hides the navigationBar (sets its frame.origin.y to a negative value, thereby sending it offscreen) when I scroll the tableView. The navigationBar is successfully hidden, but when I leave the app by tapping the home button and then launch the app into the foreground, the navigationBar is back with frame.origin at (20, 0) (x = 20 since the status bar is visible), and the tableView pushed down the screen (with frame.origin back to (64, 0) in portrait).

Any ideas on how to prevent the navigationBar from reappearing upon the app entering the foreground would be much appreciated.

minch
  • 331
  • 3
  • 13

2 Answers2

0

Instead of setting the frame you can use the below code to hide the Navigation bar.

    [self.navigationController setNavigationBarHidden:YES];

Hope this will help you. Happy coding :)

Shubhendu
  • 1,081
  • 8
  • 13
0

I think You didn't choose the best practise to hide navigationBar as most of "interal views" are positioned by it's superview automaticaly. The thing is that when you scroll, the navigationBar is moved off the screen...but when the app enters foreground all views are layouted (checkout UIView setNeedsLayout and layoutSubviews) and this moves the novigationBar back to it's position where it belongs to (y=0). There are few other options that you can use:

1) use UINavigationController's setNavigationBarHidden:(animated:) in viewWillAppear: and viewWillDisappear:. More info here SO: how to hide navigationbar when i push from navigation controller?

2) if you want to leave the space behind the navigationBar use (in viewDidLoad or methods from #1):

self.navigationController.navigationBar.alpha = 0;

3) move your code for setting frame in method viewDidLayoutSubviews of you controller (iOS 5+). Every time you scroll in UITableViewControler layoutSubviews method is called (and viewDidLayoutSubviews too). But I wouldn't recommend this solution.

Community
  • 1
  • 1
JakubKnejzlik
  • 6,363
  • 3
  • 40
  • 41
  • Sometime we do need to customize the animation, in which case setNavigationBarHidden:animated: does not provide the functionality to do so – Zheng Te Dec 27 '16 at 10:47