Chisx
Chisx

Reputation: 1986

Hide only the Navigation Bar when scrolling like Instagram iOS8

In my app I am trying to hide a UITableViewController's Navigation bar when scrolling. I have tried using self.navigationController.hidesBarsOnSwipe = YES; but it adds a very strange bottom blank space and hides the background of the UIStatusBar. I would like to, when scrolling down, hide the navBar, and when scrolling up a little, make the navBar re-appear. This behavior Im looking for is exactly like Instagram's feed, where it hides the navBar when scrolling.

Could someone guide me as to how I could achieve this with simple code and not involve some Github project or external source of any kind. I would like to eventually customize the performance to my needs. Thanks.

Upvotes: 0

Views: 648

Answers (1)

Nikhil Modi
Nikhil Modi

Reputation: 187

Register your view controller to be the UIScrollViewDelegate for example.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate (BOOL)decelerate;

From within de UIScrollViewDelegate methods you can get the new contentOffset and translate your UINavigationBar up or down accordingly.

Setting the alpha of the subviews can also be done based on some threshold values and factors you can set and compute.

Hope it helps!

OR You can also use this

@property (strong, nonatomic) NSArray *navBarItems;

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

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

    if(self.navBarItems.count > 0){
        [self.navigationController.navigationBar setItems:self.navBarItems];
    }

    [self.navigationController.navigationBar setFrame:frame];
}

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

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

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

        if(self.navigationController.navigationBar.items.count > 0){
            self.navBarItems = [self.navigationController.navigationBar.items copy];
            [self.navigationController.navigationBar setItems:nil];
        }
    }
    else if([scrollView.panGestureRecognizer translationInView:self.view].y > 0)
    {
        frame.origin.y = 20;

        if(self.navBarItems.count > 0){
            [self.navigationController.navigationBar setItems:self.navBarItems];
        }
    }

    [UIView beginAnimations:@"toggleNavBar" context:nil];
    [UIView setAnimationDuration:0.2];
    [self.navigationController.navigationBar setFrame:frame];
    [UIView commitAnimations];
}

Upvotes: 0

Related Questions