iOS:滚动表格视图时隐藏和显示自定义导航栏

时间:2016-03-29 13:34:37

标签: ios objective-c uiscrollview uinavigationbar uirefreshcontrol

我有一个自定义导航栏,我想在滚动时隐藏并在滚动停止时显示。

- (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));
}

但问题是我还使用了UIRefreshControl来提取刷新方法。当我尝试拖动tableView以刷新它时调用

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

并隐藏导航栏。有没有办法检查用户是否从屏幕顶部拉下来,即从第一个表格单元格下拉?

我试过

- (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));
    }

}

但是当用户向下滚动时,这不会隐藏导航栏。解决这个问题的任何解决方案?

2 个答案:

答案 0 :(得分:1)

只需在!=

中将条件>=更改为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));
    }

}

答案 1 :(得分:0)

您可以尝试使用以下代码,看看这是否适合您。

@property (nonatomic) CGFloat lastContentOffset; //是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 
}