4

Currently I have a UICollection in which I hide the UINavigation Top Bar when you scroll down 40px. The UINavigation Top Bar appears again when you scroll back up to the top.

This works however I want to be able to show /hide the UINavigation Top Bar anytime the user scrolls 40px and not only when you scroll from the top.

e.g When you start scrolling from the top the bar hides, You scroll to the middle of the UICollectionView and then when you strat scrolling back to the top the UINavigation Top Bar shows again.

Any ideas?

#pragma mark - UIScrollViewDelegate Methods
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
    return;
}

CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
    return;
}

CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height - 300;
if (offsetY > contentHeight - scrollView.frame.size.height)
{
    [self.homePaginator fetchNextPage];
}

CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 25;

if([scrollView.panGestureRecognizer translationInView:self.view].y < 0)
{
    frame.origin.y = -size;

    if(self.navigationController.navigationBar.items.count > 0){

        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                         animations:^{

                             CGFloat navBarHeight = 25.0f;
                             CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
                             [self.navigationController.navigationBar setFrame:frame];
                         }
                         completion:^(BOOL finished) {

                             self.navigationItem.titleView.alpha = 0;
                             self.piccingTitleIcon.alpha = 0;
                             self.navigationItem.rightBarButtonItem = nil;

                         }];
    }
}
else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
{

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         CGFloat navBarHeight = 64.0f;
                         CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
                         [self.navigationController.navigationBar setFrame:frame];
                         self.navigationItem.titleView.alpha = 1;
                         self.piccingTitleIcon.alpha = 1;
                         self.btnSearch = [[UIBarButtonItem alloc] initWithCustomView:self.searchIconButton];
                         [self.navigationItem setRightBarButtonItem:self.btnSearch];
                     }
                     completion:^(BOOL finished) {

                         [self performSelector:@selector(addIconsToNavBar) withObject:nil afterDelay:-1.0];
                     }];
}
}
CapeFurSeal
  • 183
  • 2
  • 13

3 Answers3

1

Try this!

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGPoint scrollOffset = scrollView.contentOffset;
    if (scrollOffset.y >= 40) {
        if (![self.navigationController isNavigationBarHidden]) {
            [self.navigationController setNavigationBarHidden:YES animated:YES];
        }
    } else {
        if ([self.navigationController isNavigationBarHidden]) {
            [self.navigationController setNavigationBarHidden:NO animated:YES];
        }
    }
}
LLIAJLbHOu
  • 1,313
  • 12
  • 17
  • 1
    This approach won't help because the author asked for `anytime the user scrolls 40px and not only when you scroll from the top.` Read the question more carefully. – Sergey Grischyov Jun 19 '14 at 08:30
1

Get starting content offset before user scrolls.

– scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  currentOffset = scrollView.contentOffset.y;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

CGFloat scrollPos = scrollView.contentOffset.y - currentOffset;

if(scrollPos >= 40 || scrollPos <= -40 /* or whatever the height of your toolbar is */){
    //Fully hide your toolbar
   [UIView animateWithDuration:2.25 animations:^{
    [self.navigationController setNavigationBarHidden:YES animated:YES];

}];
} else {
   //Slide it up incrementally, etc.
 [self.navigationController setNavigationBarHidden:NO animated:YES];
}
}
Parag Shinde
  • 176
  • 11
0

Anyone who wants to know a simple way to do this can use the following code.

there libraries that make it easy to do this but I found that I have to rewrite the navigation of the app with their custom class of UINavgationController If you have an existing project you can do this easier...

#pragma mark - UIScrollViewDelegate Methods
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
    return;
}

CGRect frame = self.navigationController.navigationBar.frame;
frame.origin.y = 20;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0f){
    return;
}

CGFloat offsetY = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height - 300;
if (offsetY > contentHeight - scrollView.frame.size.height)
{
    [self.homePaginator fetchNextPage];
}

CGRect frame = self.navigationController.navigationBar.frame;
CGFloat size = frame.size.height - 25;

if([scrollView.panGestureRecognizer translationInView:self.view].y < 0)
{
    frame.origin.y = -size;

    if(self.navigationController.navigationBar.items.count > 0){

        [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                         animations:^{

                             CGFloat navBarHeight = 25.0f;
                             CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
                             [self.navigationController.navigationBar setFrame:frame];
                         }
                         completion:^(BOOL finished) {

                             self.navigationItem.titleView.alpha = 0;
                             self.piccingTitleIcon.alpha = 0;
                             self.navigationItem.rightBarButtonItem = nil;

                         }];
    }
}
else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
{

    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
                     animations:^{
                         CGFloat navBarHeight = 64.0f;
                         CGRect frame = CGRectMake(0.0f, 0.0f, 320.0f, navBarHeight);
                         [self.navigationController.navigationBar setFrame:frame];
                         self.navigationItem.titleView.alpha = 1;
                         self.piccingTitleIcon.alpha = 1;
                         self.btnSearch = [[UIBarButtonItem alloc] initWithCustomView:self.searchIconButton];
                         [self.navigationItem setRightBarButtonItem:self.btnSearch];
                     }
                     completion:^(BOOL finished) {

                         [self performSelector:@selector(addIconsToNavBar) withObject:nil afterDelay:-1.0];
                     }];
}
}
CapeFurSeal
  • 183
  • 2
  • 13