ios - 滚动时收缩的标题 View

标签 ios uitableview

我想要在 table 的顶部有一个大视野。这提供了类似搜索栏的搜索参数,但有更多选项……因此占用了更多空间。 (比方说 100 像素。)

当我滚动它时,我希望它消失而只是简化。 (比方说 44 像素。)它不是可编辑的字段,而是搜索的摘要。点击该摘要将滚动回顶部,再次将搜索 View 扩展到完整大小。

我该怎么做?

最佳答案

第一步是你的标题不应该是你的 UITableView 的一部分,而是添加在同一层。

[self.view addSubview:self.tableView];
[self.view addSubview:self.myHeaderView];

要使表格正常运行,只需在表格 (tableView.tableHeaderView) 上放置一个与您需要的 View 大小相同的空标题。

UIView *emptyHeader = [[UIView alloc] initWithFrame:self.myHeaderView.bounds];
self.tableView.tableHeaderView = emptyHeader;

此外,由于自定义 header 的行为是动态的,因此您必须创建一个 UIView 子类并在那里管理您的 header 。

让您的 UITableViewDelegate 实现 scrollViewDidScroll,并在每次调用时检查 contentOffset。

假设展开 View 的高度为 100,收缩时为 44:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > (100 - 44)) {
        // Contract your header
        myHeaderView.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
        [myHeaderView contract]; // Make it display whatever data you want and setup the touch gesture
    }
    else {
        int offset = scrollView.contentOffset.y;
        // The reason for the -offset is that you need it to align to the table's real header position, which will be a little off screen
        myHeaderView.frame = CGRectMake(0, -offset, self.view.frame.size.width, 100);
        [myHeaderView expand];
    }
}

希望这有帮助:)

关于ios - 滚动时收缩的标题 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19664919/

相关文章:

ios - 动态 UITableViewCell 内容不展开单元格

ios - Google Calendar JSON 提要到 TableView

ios - 跨域资源共享问题 : both OSX Safari and iOS Safari fail after preflight request

objective-c - self 会保留在 block 内吗?

ios - 具有两个不同自定义单元格的 UITableview 在滚动时重叠

UITableView 显然因 _BSMachError 而崩溃

ios - 在 UITableViewCell 中动态布置 subview

android - 错误 : Command failed with exit code ENOENT

ios - 完整图像未加载到 UICollectionView Cell Swift 4 内的 ImageView 上

ios - 如何移动 uitableview 自定义单元格?