【问题标题】:iOS:Hide and display custom navigation bar while scrolling table viewiOS:滚动表格视图时隐藏和显示自定义导航栏
【发布时间】:2016-03-29 13:34:36
【问题描述】:

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

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

}

但是当用户向下滚动时,这不会隐藏导航栏。有什么办法可以解决这个问题?

【问题讨论】:

    标签: ios objective-c uiscrollview uinavigationbar uirefreshcontrol


    【解决方案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));
        }
    
    }
    

    【讨论】:

    • 我试过了。但是当我再次拖动表格刷新它时,它会隐藏导航栏
    • 把 else 条件和它们的 make hidden = false
    【解决方案2】:

    你可以试试下面的代码,看看这是否适合你。

    @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 
    }
    

    【讨论】:

    • 我应该在哪个代码中隐藏导航栏?我试图隐藏在这两种情况下,但又失败了
    • 仅使用答案中描述的方法,评论其他方法并请通过答案中的cmets。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多