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

前端 未结 4 971
执笔经年
执笔经年 2020-12-23 23:41

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?

相关标签:
4条回答
  • 2020-12-24 00:11

    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.

    0 讨论(0)
  • 2020-12-24 00:12
    - (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.

    0 讨论(0)
  • 2020-12-24 00:16

    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)
        }
    }
    
    0 讨论(0)
  • 2020-12-24 00:30

    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.

    0 讨论(0)
提交回复
热议问题