我正在尝试为我的 uiTableView 创建一个标题 View (不是部分标题,我已经有了这些。)我已经在界面生成器中设置了一个 XIB。所有连接都已连接好并且运行良好……除了 table 没有足够的空间!我的问题是表格的顶部与表格的标题有一点重叠。
我的 XIB 设置了所有按钮的自动布局,IB 很高兴约束没有冲突/不明确。 View 设置为自由格式大小,在我的例子中最终为 320 x 471。然后在 View 的约束中,我为相同的 View 设置了一个固有大小。
现在这与我的 table 完美搭配。一切看起来都很棒。但是,如果我使用代码手动更改 header View 中的任何字体,布局会使 View 变大,最终会出现在我的表格下方。
任何想法如何让 tableviewcontroller 在设置字体和大小后为标题 View 留出足够的空间?我希望我的解释是有意义的。
最佳答案
注意:可以在此处找到 Swift 3+ 版本:https://gist.github.com/marcoarment/1105553afba6b4900c10#gistcomment-1933639
想法是在 systemLayoutSizeFittingSize:targetSize
的帮助下计算标题的高度。
Returns the size of the view that satisfies the constraints it holds. Determines the best size of the view considering all constraints it holds and those of its subviews.
更改表头高度后,需要重新分配 tableHeaderView
属性以调整表格单元格。
基于这个答案:Using Auto Layout in UITableView for dynamic cell layouts & variable row heights
- (void)sizeHeaderToFit
{
UIView *header = self.tableView.tableHeaderView;
[header setNeedsLayout];
[header layoutIfNeeded];
CGFloat height = [header systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
CGRect frame = header.frame;
frame.size.height = height;
header.frame = frame;
self.tableView.tableHeaderView = header;
}
关于ios - 使用自动布局、IB 和字体大小时,表头 View 高度错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19005446/