ios – 如何创建其高度由其标签的高度确定的UITableView标题?

我想在我的tableView中添加一个标题.此标题包含1个UILabel.标题高度应根据标签的行数计算.

在我的代码中,我添加了与标签<>的所有边缘的约束.头.这是我的尝试:

//Add header to tableView
    header = UIView()
    header.backgroundColor = UIColor.yellowColor()
    tableView!.tableHeaderView = header

    //Create Label and add it to the header
    postBody = UILabel()
    postBody.text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
    postBody.font = UIFont(name: "Lato-Regular",size: 16.0)
    postBody.numberOfLines = 0
    postBody.backgroundColor = FlatLime()
    header.addSubview(postBody)

    //Enable constraints for each item
    postBody.translatesAutoresizingMaskIntoConstraints = false
    header.translatesAutoresizingMaskIntoConstraints = false

    //Add constraints to the header and post body
    let postBodyLeadingConstraint = NSLayoutConstraint(item: postBody,attribute: NSLayoutAttribute.Leading,relatedBy: NSLayoutRelation.Equal,toItem: header,multiplier: 1,constant: 0)
    postBodyLeadingConstraint.active = true

    let postBodyTrailingConstraint = NSLayoutConstraint(item: postBody,attribute: NSLayoutAttribute.Trailing,constant: 0)
    postBodyTrailingConstraint.active = true


    let postBodyTopConstraint = NSLayoutConstraint(item: postBody,attribute: NSLayoutAttribute.Top,constant: 0)
    postBodyTopConstraint.active = true


    let postBodyBottomConstraint = NSLayoutConstraint(item: postBody,attribute: NSLayoutAttribute.Bottom,constant: 0)
    postBodyBottomConstraint.active = true


    //Calculate header size
    let size = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
    var frame = header.frame
    frame.size.height = size.height
    header.frame = frame
    tableView!.tableHeaderView = header
    header.layoutIfNeeded()

这是我的表:

let nib = UINib(nibName: "MessagesTableViewCell",bundle: nil)
    let nibSimple = UINib(nibName: "SimpleMessagesTableViewCell",bundle: nil)
    self.tableView!.registerNib(nib,forCellReuseIdentifier: "MessagesTableViewCell")
    self.tableView!.registerNib(nibSimple,forCellReuseIdentifier: "SimpleMessagesTableViewCell")
    self.tableView!.dataSource = self
    self.tableView!.delegate = self
    self.tableView!.rowHeight = UITableViewAutomaticDimension
    self.tableView!.estimatedRowHeight = 100.0
    self.tableView!.separatorStyle = UITableViewCellSeparatorStyle.None
    self.tableView!.separatorColor = UIColor(hex: 0xf5f5f5)
    self.tableView!.separatorInset = UIEdgeInsetsMake(0,0)
    self.tableView!.clipsToBounds = true
    self.tableView!.allowsSelection = false
    self.tableView!.allowsMultipleSelection = false
    self.tableView!.keyboardDismissMode = .OnDrag

你可以看到,标题没有考虑标签的高度(我做了numberOfLines = 0)

解决方法

UILabels利用UIView的intrinsicContentSize()来告诉自动布局应该是什么大小.然而,对于多行标签,内在内容大小是不明确的;桌子不知道它是否应该是短而宽,高和窄,或两者之间的任何东西.

为了解决这个问题,UILabel有一个名为preferredMaxLayoutWidth的属性.设置这个告诉一个多行标签,它最多应该是这个宽度,并允许intrinsicContentSize()找出并返回一个适当的高度来匹配.通过在示例中不设置preferredMaxLayoutWidth,标签将其宽度无限制,因此计算长单行文本的高度.

preferredMaxLayoutWidth唯一的复杂因素是,您通常不知道您希望标签的宽度,直到自动布局为您计算一个.因此,将视图控制器子类(看起来像代码示例可能来自)的位置在viewDidLayoutSubviews中:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    postBody.preferredMaxLayoutWidth = CGRectGetWidth(postBody.frame)
    // then update the table header view
    if let header = tableView?.tableHeaderView {
        header.frame.size.height = header.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize).height
        tableView?.tableHeaderView = header
    }
}

显然,您需要为postBody标签添加一个属性才能使其工作.

让我知道,如果你不在这里的UIViewController子类,我会编辑我的答案.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...