是否有可能获得使用自动布局动态表视图节头的高度? [英] Is it possible to obtain a dynamic table view section header height using Auto Layout?

查看:146
本文介绍了是否有可能获得使用自动布局动态表视图节头的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在新的iOS 8中,您可以通过简单的设置估计行高获得100%的动态表视图单元格,然后选择布局在使用自动布局单元格的元素。如果在高度含量的增加,细胞也将增加的高度。这是非常有用的,而且我想知道如果同样的壮举可以为节头在表视图来完成?

New in iOS 8, you can obtain 100% dynamic table view cells by simply setting the estimated row height, then layout your elements in the cell using Auto Layout. If the content increases in height, the cell will also increase in height. This is extremely useful, and am wondering if the same feat can be accomplished for section headers in a table view?

可以之一,例如,创建一个的UIView 的tableView:viewForHeaderInSection:,添加一个的UILabel 子视图,指定自动布局限制了对视图的标签,并在高度视图增加,以适应标签的内容,而无需执行的tableView:heightForHeaderInSection:

Can one, for example, create a UIView in tableView:viewForHeaderInSection:, add a UILabel subview, specify auto layout constraints for the label against the view, and have the view increase in height to fit the label's contents, without having to implement tableView:heightForHeaderInSection:?

viewForHeaderInSection 的文件指出:此方法仅适用正确时的tableView:heightForHeaderInSection:也可以实现我没有听说如果有什么改变了针对iOS 8。

The documentation for viewForHeaderInSection states: "This method only works correctly when tableView:heightForHeaderInSection: is also implemented." I haven't heard if anything has changed for iOS 8.

如果一个人不能做的,什么是模仿这种行为的最好方法?

If one cannot do that, what is the best way to mimic this behavior?

推荐答案

这是可能的。它是在旁边的iOS 8.不知道我怎么错过了实现动态单元格的高度新的权利。

This is possible. It is new right alongside the dynamic cell heights implemented in iOS 8. Not sure how I missed it.

这是非常简单的。就在添加此 viewDidLoad中

It's extremely simple. Just add this in viewDidLoad:

self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableView.estimatedSectionHeaderHeight = 25;

然后重写 viewForHeaderInSection 并使用自动布局认为应该限制元素的视图。完成。没有必要实施 heightForHeaderInSection 。而实际上在 sectionHeaderHeight 不需要任何说明,我只是增加了它的清晰度。

Then override viewForHeaderInSection and use Auto Layout to constrain elements in your view as seen fit. Done. No need to implement heightForHeaderInSection. And actually the sectionHeaderHeight need not be stated either, I just added it for clarity.

这篇关于是否有可能获得使用自动布局动态表视图节头的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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