Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode/iOS: How to hide Navigation- AND ToolBar on scroll down?

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?

like image 387
filou Avatar asked Apr 16 '12 19:04

filou


People also ask

How do I hide Show view when scrolling up in Swift?

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.

How do you hide the navigation title in Swift?

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.

What is scroll view in Xcode?

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.


2 Answers

- (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.

like image 184
mdominick Avatar answered Sep 28 '22 10:09

mdominick


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.

like image 20
vikingosegundo Avatar answered Sep 28 '22 08:09

vikingosegundo