我正在尝试为我的 uiTableView 创建一个标题视图(不是部分标题,我已经有了这些.)我已经在界面生成器中设置了一个 XIB.所有的连接都连接好了,而且运行得很漂亮……除了桌子没有给它足够的空间!我的问题是表格的顶部与表格的标题有一点重叠.
I am trying to create a header view for my uiTableView (not a section header, I already have those.) I have set up an XIB in interface builder. All the connections are hooked up and it runs beautifully... except the table doesn't give it enough room! My problem is that the top of the table overlaps the table's header by a little.
我的 XIB 设置了所有按钮的 autlayout,IB 很高兴约束不会冲突/模棱两可.视图设置为自由格式大小,在我的情况下最终为 320 x 471.然后在视图的约束中,我为相同的视图设置了固有大小.
My XIB is setup with autlayout for all the buttons, and IB is happy that the constraints don't conflict / ambiguous. The view is set to Freeform size, which in my case ended up being 320 x 471. Then in constraints for the view, I set an intrinsic size for the view of the same.
现在这与我的桌子完美搭配.一切看起来都很棒.但是,如果我使用代码手动更改标题视图中的任何字体,布局会使视图变大,并最终出现在我的表格下方.
Now this works perfectly with my table. Everything looks great. But if I manually change any of the fonts in the header view with code, the layout makes the view bigger, and it ends up underneath my table.
在设置字体和大小后如何让 tableviewcontroller 为标题视图留出足够的空间?我希望我的解释是有意义的.
Any ideas how to get the tableviewcontroller to leave enough room for the header view after setting fonts and sizes? I hope I've made sense explaining this.
注意:可以在此处找到 Swift 3+ 版本:https://gist.github.com/marcoarment/1105553afba6b4900c10#gistcomment-1933639
Note: A Swift 3+ version can be found here: https://gist.github.com/marcoarment/1105553afba6b4900c10#gistcomment-1933639
这个想法是在 systemLayoutSizeFittingSize:targetSize
的帮助下计算标题的高度.
The idea is to calculate header's height with help of 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
属性来调整表格单元格.
After changing header's height it is necessary to reassign tableHeaderView
property to adjust table cells.
基于此答案:在 UITableView 中使用自动布局进行动态单元格布局 &可变行高
- (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;
}
这篇关于使用自动布局、IB 和字体大小时表头视图高度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!