-systemLayoutSizeFittingSize:在iOS 8下为tableHeaderView返回不正确的高度 [英] -systemLayoutSizeFittingSize: returning incorrect height for tableHeaderView under iOS 8
问题描述
关于使用自动布局正确调整tableHeaderView大小的众多线程(
There are numerous threads about correctly sizing a tableHeaderView with auto-layout (one such thread) but they tend to pre-date iOS 8.
我遇到了很多表视图和标题的情况,该视图在iOS 7下大小正确,但在iOS 8下使用前面提到的大多数线程拥护的代码却不正确.在表的控制器中,我有以下方法:
I have a situation with numerous table views, all with headers, that size correctly under iOS 7 but incorrectly under iOS 8 using the code that most of the aforementioned threads champion. In the controllers for the tables, I have the following method:
- (void)rejiggerTableHeaderView
{
self.tableView.tableHeaderView = nil;
UIView *header = self.headerView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = header.frame;
headerFrame.size.height = height;
header.frame = headerFrame;
self.tableView.tableHeaderView = header;
}
在iOS 7下使用多行标签,这样可以正确调整表格视图标题的大小,如下所示:
With a multi-line label under iOS 7, this correctly sizes the table view's header like so:
但是在iOS 8下运行的相同代码会产生以下结果:
But the same code run under iOS 8 produces the following:
获取-systemLayoutSizeFittingSize:在iOS 8下返回正确大小的诀窍是什么? 这是一个演示问题的示例项目.
What's the trick to getting -systemLayoutSizeFittingSize: to return the correct size under iOS 8? Here's a sample project that demonstrates the issue.
推荐答案
将headerview函数更改为以下对我有用的东西:
Changing your headerview function to the following works for me:
- (void)rejiggerTableHeaderView
{
self.tableView.tableHeaderView = nil;
UIView *header = self.headerView;
CGRect frame = header.frame;
frame.size.width = self.tableView.frame.size.width;
header.frame = frame;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect headerFrame = header.frame;
headerFrame.size.height = height;
header.frame = headerFrame;
self.tableView.tableHeaderView = header;
}
这篇关于-systemLayoutSizeFittingSize:在iOS 8下为tableHeaderView返回不正确的高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!