使用UISearchBar时,UITableView的contentSize不正确

8

UITableView在使用UISearchBar时报告了比预期更大的contentSize。如果没有单元格,预期的内容高度应为零。但是,在运行iOS 7的iPhone 4英寸设备上,以下代码输出612。

@implementation HPViewController {
    UITableView *_tableView;
    UISearchBar *_searchBar;
    UISearchDisplayController *_searchController;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tableView];

    _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];

    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchController.delegate = self;
    _searchController.searchResultsDataSource = self;
    _tableView.tableHeaderView = _searchBar;
}

- (void)viewDidLayoutSubviews
{
    CGSize contentSize = _tableView.contentSize;
    NSLog(@"%f", contentSize.height); // 612.000000
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 0; }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { return nil; };

@end

注释掉设置头部视图的代码行后,代码输出为0,如预期所示。

此外,如果我将空的UIView分配为标题,则contentSize将是正确的,并且与标题的高度相匹配。这个问题只会出现在UISearchBar中。

有没有什么解决方法?我做错了什么吗?


1
今天我在iOS 11.2上遇到了同样的问题。在表格中只有1个标题和1个单元格时,我几乎可以将所有内容滚动到屏幕外。然而,在iOS 10.3.1上,contentSize具有我期望的高度。 - Zach
3个回答

7

UISearchBar放置在一个容器UIView中可以大部分解决这个问题。

- (void)viewDidLoad
{
    [super viewDidLoad];
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:_tableView];

    UIView *searchBarContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    _searchBar = [[UISearchBar alloc] initWithFrame:searchBarContainer.bounds];
    [searchBarContainer addSubview:_searchBar];

    _searchController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
    _searchController.delegate = self;
    _searchController.searchResultsDataSource = self;
    _tableView.tableHeaderView = searchBarContainer;
}

很遗憾,UISearchBar在某些情况下会出现故障,我尚未找到原因。我选择通过添加所有单元格的高度来手动计算contentSize


1
感谢您提供将子视图添加到视图的提示。对我而言,这个方法已经有效了。 - Brian F Leighty
搜索栏会自动隐藏在导航栏下面,但由于视图容器的原因,它不幸消失了... - aquarium_moose

3

iOS11起,UITableView默认使用估算的行/头部/尾部高度来计算初始的contentSize。实际上,如果您点击搜索栏并关闭键盘,content size就会有正确的值。

要修复这个问题,请在IB中将估算行、头部和尾部高度设置为0,而不是自动计算:

enter image description here

或者直接在代码中进行设置:

self.tableView.estimatedRowHeight = 0;
self.tableView.estimatedSectionHeaderHeight = 0;
self.tableView.estimatedSectionFooterHeight = 0;

-1
请添加此委托方法。
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc] initWithFrame:CGRectZero];
}

也许您的页脚视图增长是因为没有单元格可显示。


那不会被称为“section”。我的示例代码中没有“sections”。标题是表视图的标题,而不是节标题。 - hpique

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接