Reputation: 1619
I would like to hide both bars on scroll down on my iPhone. When I scroll up, they should appear again.. How can I handle this?
Upvotes: 10
Views: 19183
Reputation: 7708
You might check this, available from iOS8, i think this is the reverse of what you are looking for...but worth checking as it is something standard and this is how Safari works.
Swift
var hidesBarsOnSwipe: Bool
Objective-C
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe Discussion
When this property is set to YES, an upward swipe hides the navigation bar and toolbar. A downward swipe shows both bars again. If the toolbar does not have any items, it remains visible even after a swipe. The default value of this property is NO.
Upvotes: 0
Reputation: 1044
Here's my solution in Swift; it works beautifully
func scrollViewDidScroll(scrollView: UIScrollView) {
let navController: UINavigationController = self.navigationController!
if self.collectionView.panGestureRecognizer.translationInView(self.view).y <= 0.0 {
defaultCenter.postNotificationName("stuffShouldHide", object: self)
} else {
defaultCenter.postNotificationName("stuffShouldUnhide", object: self)
}
}
Upvotes: 0
Reputation: 1319
- (void)scrollViewWillBeginScroll :(UIScrollView *)scrollView {
if (scrollView.contentOffset.y < lastOffset.y) {
[toolBar setHidden:YES];
[[[self navigationController] navigationBar] setHidden:YES];
} else{
// unhide
}
}
- (void)scrollViewDidScroll :(UIScrollView *)scrollView {
/// blah blah
lastOffset = scrollView.contentOffset;
}
Note : lastOffset
is an CGPoint
and it goes in your header file: @Interface
.
Upvotes: 7
Reputation: 52227
The accepted answer does not work for me, as scrollViewWillBeginScroll:
is not a delegate method.
Instead I do
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldHide" object:self];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView
willDecelerate:(BOOL)decelerate
{
if(!decelerate)
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide"
object:self];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"BarsShouldUnhide"
object:self];
}
Anywhere in the app objects can listen for this notification,like
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldHide"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
//hide tab bar with animation;
}];
[[NSNotificationCenter defaultCenter] addObserverForName:@"BarsShouldUnhide"
object:nil
queue:nil
usingBlock:^(NSNotification *note) {
//Unhide tab bar with animation;
}];
}
This code will hide the bars for any scroll. if you want to have only on down, the same locationOffset
trick as in the accepted answer should work.
Upvotes: 4