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
中。
有没有什么解决方法?我做错了什么吗?