我遇到了 UITableView
及其属性 tableHeaderView
的问题。
我想让 tableHeaderView
表现得像 UISearchBar
,即内容偏移量应该是 tableHeaderView 的偏移量。
当表格 View 无法填充 View 的框架时,设置 contentOffset 等没有帮助。
这是当前行为的屏幕截图:
(来源:tubtub.de)
我希望它如何:
(来源:tubtub.de)
我在 viewDidLoad
中插入 headerView
,如下所示:
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
}
非常感谢任何帮助或提示。谢谢!
编辑:我通过使用 UIScrollViewDelegate 和子类化 UITableView 使其工作。在 github repo 上查看提供。
最佳答案
您可以使用 setContentOffset: by headerView 高度。 这个 contentOffset 只会在你有足够数量的数据被你的 headerView 高度关闭时发生。即)如果 tableView 不可滚动,因为您的屏幕截图中只有很少的数据,则 headerView 仍然可见。但是,如果您有大量无法在屏幕上显示的数据,它将具有 contentOffset。
用 20 行试试这个。你会明白我的意思。
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
[self.tableView setContentOffset:CGPointMake(0, 44)];
只有 3 行时,此 contentOffset 将不起作用,并且在您的情况下这可能是您希望的行为,因为您不想在有额外空间显示时隐藏 searchBar。
关于ios - UITableView headerView 滚动偏移量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23667518/