4

我有 UITableview 控制器,它在标题中有搜索栏。当我向上滚动表格视图并反弹时。但是搜索栏隐藏了。当我向下滚动然后显示搜索栏。谁能告诉我如何再次显示搜索栏?

我已经尝试了以下代码,但它不能顺利运行:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        //scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);
        [self.tableView setContentOffset:newOffset animated:YES];
    }
}

这是滚动前的屏幕截图: 在此处输入图像描述

这是错误的观点,滚动后:

在此处输入图像描述

4

2 回答 2

1

如何禁用动画关闭并像这样放置自定义动画:

还要确保使用 MAcro 具有相同的 sectionHeader 高度。我仍然怀疑你的评论说 35 或 25。

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
            CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
            if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {

    [UIView animateWithDuration: 1.0
                      animations: ^{
                          scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
                      }completion: ^(BOOL finished){
                      }
        ];
            } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
                CGPoint newOffset = CGPointMake(0, -[self.tableView  contentInset].top);


[UIView animateWithDuration: 1.0
                  animations: ^{
                      [self.tableView setContentOffset:newOffset animated:NO];
                  }completion: ^(BOOL finished){
                  }
    ];
            }
}
于 2016-05-19T11:00:37.573 回答
0

试试这个代码

- (void)scrollViewDidScroll:(UIScrollView *)scrollView  
{  
    CGFloat sectionHeaderHeight = 40;  
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {  
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);  
    }  
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {  
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);  
    }  
} 

但您的问题是,您的 tableview 标题不是 40,而是 35 或 25,这意味着您的 tableview 无法为插图设置正确的数字,请尝试将 40 更改为正确的标题高度。

于 2016-05-19T10:42:54.953 回答