I'm trying to use the new feature added in iOS 8 - hiding the navigation bar while user is scrolling the table view (similar to what mobile Safari does). I'm setting the property hidesBarsOnSwipe
of UINavigationController
to YES
in viewDidAppear
method of UITableViewController
:
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if([self.navigationController respondsToSelector:@selector(hidesBarsOnSwipe)]) {
self.navigationController.hidesBarsOnSwipe = YES;
}
}
The navigation bar hides when the view is being scrolled. So far so good. But the status bar is still visible and my table view contents show through it, which looks ugly:
I tried setting edgesForExtendedLayout
to UIEdgeRectNone
or adjusting the contentInset
of the table view, but it didn't help. Is there any other solution to hide the status bar along with the navigation bar, or make it opaque?
Okay I spent all day doing this, hopefully this helps some people out. There's a
barHideOnSwipeGestureRecognizer
. So you could make a listener for the correspondingUIPanGesture
, noting that if the navigation bar is hidden then its y origin is -44.0; otherwise, it's 0 (not 20 because we hid the status bar!).In your view controller (swift 2):
After much struggle, I was finally able to solve this.
Add the following code to the UIViewController's viewDidLoad function:
What you are doing is creating a solid bar right beneath the navigation bar. As the navigation bar moves up, the solid bar move up too and right behind the status bar.
The only problem is that when you rotate the screen side ways, the white bar still there even though the status bar disappears.
You need to connect navigation isNavigationBarHidden property with status bar.
The answer from @iOSergey works perfect!
Here is the solution in Swift 1.2. Add the following code to the views .swift file:
Building off of anas' answer, I have a working solution (I'm assuming
tableViewController
is yourUITableViewController
instance):In a
UINavigationController
subclass (or also potentially fromtableViewController
):In your
tableViewController
:Let me know if this doesn't work for you. A few non-obvious things:
self.navigationController.navigationBar.frame.origin.y
was -44 (the negative height of the navigation bar) when hidden, and 20 (the height of the status bar) when visible. There was no in-between, even during animations, so a negative value == hidden and a nonnegative value == visible.UIViewController
within aUINavigationController
within aUITabBarController
, and it didn't work until I overrodeprefersStatusBarHidden
on theUIViewController
.setNeedsStatusBarAppearanceUpdate
in an animation block.Actually it is pretty easy to do. You just need to connect navigation isNavigationBarHidden property with status bar.
Objective-C
Swift <= 2.3
Swift 3.0
And be sure you have "View controller-based status bar appearance" = "YES" in your application .plist file.