How to prevent status bar from overlapping content with hidesBarsOnSwipe set on UINavigationController?
Building off of anas' answer, I have a working solution (I'm assuming tableViewController
is your UITableViewController
instance):
In a UINavigationController
subclass (or also potentially from tableViewController
):
- (void)viewDidLoad {
if ([self respondsToSelector:@selector(barHideOnSwipeGestureRecognizer)]) {
// iOS 8+
self.hidesBarsOnSwipe = YES;
[self.barHideOnSwipeGestureRecognizer addTarget:self action:@selector(swipe:)];
}
}
- (void)swipe:(UISwipeGestureRecognizer *)recognizer {
BOOL shouldHideStatusBar = self.navigationController.navigationBar.frame.origin.y < 0;
tableViewController.hideStatusBar = shouldHideStatusBar;
[UIView animateWithDuration:0.2 animations:^{
[tableViewController setNeedsStatusBarAppearanceUpdate];
}];
}
In your tableViewController
:
@property(nonatomic, getter = shouldHideStatusBar) BOOL hideStatusBar;
- (BOOL)prefersStatusBarHidden {
return [self shouldHideStatusBar];
}
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.- The child view controller is the one queried for whether or not the status bar should be hidden. In my case, I have a
UIViewController
within aUINavigationController
within aUITabBarController
, and it didn't work until I overrodeprefersStatusBarHidden
on theUIViewController
. - Since a hidden status bar has no frame, your content might jerk upwards 20 points unless you wrap the call to
setNeedsStatusBarAppearanceUpdate
in an animation block. - Hopefully the syntax is correct; I backported this from my Swift code.
How to stop an uitableview housed in tabbarcontrol from overlapping the status bar?
Do you use Storyboards and autolauyout?
If so, just:
Select the ViewController in storyboard
select attribute inspector
Status Bar default
Select The tableView and set the top constraint to top Layout guide
If its not working this way plan B would be to use a normal ViewController, add a container to it, embed your TableViewController into the container and do the above stuff on the ViewController.
Swift iOS -NavigationBar hidesBarsOnSwipe never reappears when setting CollectionView Frame under Status Bar
It was a simple fix. I followed this answer.
override var prefersStatusBarHidden: Bool {
return navigationController?.isNavigationBarHidden ?? false
}
And be sure you have "View controller-based status bar appearance" = "YES" in your application .plist file.
Hide status bar whenever nav bar is hidden - SWIFT iOS8
Sorry if this answer is a little late, but here is one way to do it.
Use the prefersStatusBarHidden() method within your view controller.
override func prefersStatusBarHidden() -> Bool {
if self.navigationController?.navigationBarHidden == true {
return true
} else {
return false
}
}
Basically says that when the Nav bar is hidden, then the status bar is too and vice versa.
Hide navigation bar when scrolling without hiding the status bar and its background
please add this method
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
in swift
func scrollViewWillBeginDecelerating(scrollView: UIScrollView)
{
}
iOS TableViewController Scrolling away content and navigation bar under status bar
When creating this kind of structure (table + other elements), the best approach is to actually have a ViewController
that contains a TableView
and additional elements (like your navigation). Do not try to put elements, that are not part of the table itself inside the table.
Here is an example of the structure, where I have intentionally coloured the custom view (in your case the navigation) orange. As you can see, it is outside the table. This way the showing and scrolling of the table is completely separated from the other elements.
Related Topics
iPhone Different Screen Sizes in Flash? (Getting Black Bars)
Presenting Uiviewcontroller from Skscene
What Is Most Common and Correct Practice to Get a Cgfloat from an Nsnumber
Swift Override Function in Extension
Set Custom Images to the Uibarbuttonitem But It Doesn't Show Any Image
What Is Build Object File Extension in iOS
Why Is This Code Not Recognising the Nsstring as Being Equal
Datefromstring Returns Nil for Some Values
Async Request Does Not Enter Completion Block
"Too Many Symbol Files" After Successfully Submitting My Apps
Run iPhone as an Ibeacon in the Background
Application Not Automatically Launching in Device and Getting Error on Xcode
Add a Watermark on Video After Merging Video and Audio Asset into One in Swift3 iOS
Dynamically Increase Height of Uilabel & Tableview Cell
How Come I Can Cast to Nsmanagedobject But Not to My Entity's Type