UILabel in UITableViewCell with auto layout has wrong height

前端 未结 6 979
孤独总比滥情好
孤独总比滥情好 2021-02-01 19:00

I have a UITableView with cells that have a fixed height of 100 points. The cells are created in a xib file that uses 3 constraints to pin a UILabel to

6条回答
  •  故里飘歌
    2021-02-01 19:39

    I know this is an old issue, but maybe this UILabel subclass can also help for some:

    class AutoSizeLabel: UILabel {
        override var bounds: CGRect {
            didSet {
                if bounds.size.width != oldValue.size.width {
                    self.setNeedsUpdateConstraints()
                }
            }
        }
    
        override func updateConstraints() {
            if self.preferredMaxLayoutWidth != self.bounds.size.width {
                self.preferredMaxLayoutWidth = self.bounds.size.width
            }
            super.updateConstraints()
        }
    }
    

    Note: works also for cases when your UILabel won't size itself correctly when inside of a StackView

提交回复
热议问题