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?
What you need to do is lock the view from animating once an animation is in progress and open it up once it is completed. If you can show an image of how top view, second view and scroll are laid out on your storyboard (not just the view hierarchy), I might be able to suggest an example.
There's a really easy way to make this happen, and it's a property on UINavigationController called hidesBarsOnTap . When this is set to true, the user can tap anywhere on the current view controller to hide the navigation bar, then tap again to show it.
The scroll view displays its content within the scrollable content region. As the user performs platform-appropriate scroll gestures, the scroll view adjusts what portion of the underlying content is visible. ScrollView can scroll horizontally, vertically, or both, but does not provide zooming functionality.
- (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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With