UITableViewAutomaticDimension不调整为UILabel文本高度

时间:2017-08-07 21:41:49

标签: ios swift uitableview

我有一个消息传递应用,并且在气泡视图的每一侧都将标签约束设置为8。我没有设置高度限制,并试图弄清楚为什么标签在滚动时在某些单元格上随机显示更多高度和错误的大小:

admins-MacBook-Pro:javascript tester$ ls
bower.json      bower_components    gulpfile.js     lib         node_modules        package.json
admins-MacBook-Pro:javascript tester$ gulp
[14:37:21] Using gulpfile ~/Documents/cakeshop/cakeshop-client-js/src/main/javascript/gulpfile.js
[14:37:21] Starting 'compile-libs'...
[14:37:21] Starting 'compile-combined'...
[14:37:21] Finished 'compile-libs' after 253 ms
[14:37:24] Finished 'compile-combined' after 3.28 s
[14:37:24] Starting 'compile'...
[14:37:24] Finished 'compile' after 27 μs
[14:37:24] Starting 'default'...
[14:37:24] Finished 'default' after 4.86 μs
admins-MacBook-Pro:javascript tester$

标签设置为行数0和自动换行。

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 30

我也尝试在设置标签后立即使用sizeToFit(),但这也不起作用。见附图。enter image description here

1 个答案:

答案 0 :(得分:0)

内容拥抱优先权解决了这个问题。

if msgUsername == UserDefaults.standard.value(forKey: USERNAME) as? String {

        recipientLbl.setContentHuggingPriority(250, for: UILayoutConstraintAxis.vertical)
        senderLbl.setContentHuggingPriority(750, for: UILayoutConstraintAxis.vertical)
        senderLbl.text = message.text
        senderLbl.sizeToFit()
        recipientUsernameLbl.isHidden = true
        recipientBubbleView.isHidden = true
        senderBubbleView.isHidden = false

    } else {

        senderLbl.setContentHuggingPriority(250, for: UILayoutConstraintAxis.vertical)
        recipientLbl.setContentHuggingPriority(750, for: UILayoutConstraintAxis.vertical)
        recipientLbl.text = message.text
        recipientUsernameLbl.text = msgUsername
        recipientUsernameLbl.isHidden = false
        recipientLbl.sizeToFit()
        senderBubbleView.isHidden = true
        recipientBubbleView.isHidden = false
    }