Search code examples
iosxcodeuiscrollviewcontentoffset

Navigation- & Toolbar not hidden when UIScrollView scrolls


- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    lastOffset = scrollView.contentOffset;

    if (scrollView.contentOffset.y < lastOffset.y) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        [self.navigationController setToolbarHidden:YES animated:YES];
    }
    else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
        [self.navigationController setToolbarHidden:NO animated:YES];
    }
}

What am I doing wrong? The UIScrollViewDelegate is already set in my header file.


Solution

  • You should move

    lastOffset = scrollView.contentOffset;
    

    to the end of the method, otherwise

    scrollView.contentOffset.y < lastOffset.y
    

    will never be true.