使用自动布局、IB 和字体大小时,表格标题视图高度错误 [英] table header view height is wrong when using auto layout, IB, and font sizes

查看:36
本文介绍了使用自动布局、IB 和字体大小时,表格标题视图高度错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的 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 为所有按钮设置了自动布局,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 和字体大小时,表格标题视图高度错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆