简体   繁体   English

UITableView headerView滚动偏移量

[英]UITableView headerView scroll offset

I'm facing a problem with UITableView and it's property tableHeaderView . 我面临UITableView及其属性tableHeaderView

I want to have the tableHeaderView to behave like UISearchBar , ie the content offset should be the of tableHeaderView. 我想让tableHeaderView表现得像UISearchBar ,即内容偏移量应该是tableHeaderView的。 Setting contentOffset, etc. didn't help when the table view wouldn't fill the view's frame. 当表格视图无法填充视图框架时,设置contentOffset等无济于事。

Here's a screenshot of the current behavior: 这是当前行为的屏幕截图:

当前行为
(source: tubtub.de ) (来源: tubtub.de

And how I'd like it to have: 以及我希望它如何:

期望的行为
(source: tubtub.de ) (来源: tubtub.de

I'm inserting the headerView in viewDidLoad as follows: 我在viewDidLoad插入headerView ,如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    headerView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = headerView;
}

Any help or hint is highly appreciated. 任何帮助或提示,我们将不胜感激。 Thanks! 谢谢!

EDIT: I made it working by utilizing UIScrollViewDelegate and subclassing UITableView. 编辑:我通过利用UIScrollViewDelegate和子类化UITableView使其工作。 Check it out on the github repo provided. 在提供的github仓库中查看。

You can use setContentOffset: by headerView height. 您可以使用setContentOffset:按headerView的高度。 This contentOffset will only happen when you have enough number of data to be off by your headerView height. 仅当您有足够数量的数据要根据headerView高度偏移时,才会发生contentOffset。 ie) If tableView is not scrollable because you have only few data like one in your screenshot, the headerView is still visible. ie)如果tableView因为在屏幕截图中只有很少的数据(例如一个)而无法滚动,则headerView仍然可见。 However if you have lots of number of data that can't not be displayed in the screen it will have contentOffset. 但是,如果您有大量无法在屏幕上显示的数据,它将具有contentOffset。

Try this with 20 rows. 尝试20行。 You will see what I mean. 您会明白我的意思。

UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
headerView.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = headerView;
[self.tableView setContentOffset:CGPointMake(0, 44)];

With only 3 rows, this contentOffset won't work and it might be desired behavior in your case because you don't want to hide the searchBar when you have extra space to display. 只有3行,此contentOffset将不起作用,并且在您的情况下可能是所需的行为,因为当您有多余的显示空间时,您不想隐藏searchBar。

I came up with implementing my own solution. 我想出了实施自己的解决方案的方法。

I utilize UIScrollViewDelegate and came up with a subclass of UITableView for calculating the contentSize dynamically. 我利用UIScrollViewDelegate并提出了UITableView的子类来动态计算contentSize

Take a look at the github repo here . 这里看看github仓库。

I would double check the frame for the table itself. 我会仔细检查表格本身的框架。 By setting the headerView's frame to start at 0,0, you are specifying that it should have its origin in the top left of the the view that is designated for the table. 通过将headerView的框架设置为从0,0开始,可以指定它的原点应位于为表指定的视图的左上方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM