是否可以使用自动布局获得动态表格视图部分标题高度? [英] Is it possible to obtain a dynamic table view section header height using Auto Layout?

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

问题描述

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?

例如可以在tableView:viewForHeaderInSection:中创建一个UIView,添加一个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 introduced in iOS 8.

为此,请为节标题高度使用自动尺寸,如果需要,您可以提供估计的节标题高度.这可以在选择表格视图或以编程方式在 Interface Builder 中完成:

To do this, use automatic dimension for the section header height, and if desired you can provide an estimated section header height. This can be done in Interface Builder when the table view is selected or programmatically:

tableView.sectionHeaderHeight = UITableView.automaticDimension
tableView.estimatedSectionHeaderHeight = 38

//You can use tableView(_:heightForHeaderInSection:) and tableView(_:estimatedHeightForHeaderInSection:)
//if you need to support different types of headers per section

然后实现 tableView(_:viewForHeaderInSection:) 并根据需要使用自动布局来约束视图.确保完全约束到 UITableViewHeaderFooterViewcontentView,尤其是从上到下,这样高度可以由约束来确定.就是这样!

Then implement tableView(_:viewForHeaderInSection:) and use Auto Layout to constrain views as desired. Be sure to fully constrain to UITableViewHeaderFooterView's contentView, especially top-to-bottom so the height can be determined by the constraints. That's it!

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {    
    let headerView = UITableViewHeaderFooterView()
    headerView.translatesAutoresizingMaskIntoConstraints = false
    headerView.backgroundView = {
        let view = UIView()
        view.backgroundColor = myCustomColor
        return view
    }()

    let headerLabel = UILabel()
    headerLabel.translatesAutoresizingMaskIntoConstraints = false
    headerLabel.text = "Hello World"
    headerView.contentView.addSubview(headerLabel)
    
    NSLayoutConstraint.activate([
        headerLabel.leadingAnchor.constraint(equalTo: headerView.contentView.leadingAnchor, constant: 16),
        headerLabel.trailingAnchor.constraint(equalTo: headerView.contentView.trailingAnchor, constant: -16),
        headerLabel.topAnchor.constraint(equalTo: headerView.contentView.topAnchor, constant: 12),
        headerLabel.bottomAnchor.constraint(equalTo: headerView.contentView.bottomAnchor, constant: -12)
    ])
    
    return headerView
}

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

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