JMS
JMS

Reputation: 281

iOS:Hide and display custom navigation bar while scrolling table view

I have a custom navigation bar which I'm trying to hide on scrolling and displaying when scrolling stops.

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
        self.navigationBView.hidden = YES;
        self.bTableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.navigationBView.hidden = NO;
     self.bTableView.frame = CGRectMake(0, CGRectGetHeight(self.navigationBView.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame) - CGRectGetHeight(self.navigationBView.frame));
}

But problem is that I have also used a UIRefreshControl for pull to refresh method. When I try to drag the tableView for refreshing it calls

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

and hides the navigation bar. Is there a method to check if user is pulling down from top of the screen i.e from the first table cell?

I tried doing

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y != 0)
    {
        self.navigationBView.hidden = YES;
        self.bTableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    }

}

But this does not hide navigation bar when user is scrolling down slowing. Any solution to solve this?

Upvotes: 0

Views: 940

Answers (2)

dev gr
dev gr

Reputation: 2441

You can try with below code, see if this works for you.

@property (nonatomic) CGFloat lastContentOffset; //is an iVar

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    float movement = fabsf(self.lastContentOffset-scrollView.contentOffset.y);
    if (self.lastContentOffset > scrollView.contentOffset.y)
    {
        //user is scrolling up through the list
        if (movement > 15 && movement < 40)
        {
            //show the navigation bar 
        }

    }
    else if (self.lastContentOffset < scrollView.contentOffset.y)
    {
        //user is scrolling down through the list
        if (movement > 15 && movement < 40)
        {
           //show the navigation bar 
        }
    }

    self.lastContentOffset = scrollView.contentOffset.y;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{    
   //show the navigation bar 
}

Upvotes: 0

Jaydeep Patel
Jaydeep Patel

Reputation: 1739

just change your condition != to >= in scrollViewWillBeginDragging

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y >= 0)
    {
        self.navigationBView.hidden = YES;
        self.bTableView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame));
    }

}

Upvotes: 1

Related Questions