UILabel in UITableViewCell with auto layout has wrong height

前端 未结 6 947
孤独总比滥情好
孤独总比滥情好 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:26

    Adding this in the cell subclass works:

    - (void)layoutSubviews
    {
        [super layoutSubviews];
        [self.contentView layoutIfNeeded];
        self.myLabel.preferredMaxLayoutWidth = self.myLabel.frame.size.width;
    }
    

    I found this on http://useyourloaf.com/blog/2014/02/14/table-view-cells-with-varying-row-heights.html.

    Update 1: This answer was for iOS 7. I find auto layout in table view cells to be very unreliable since iOS 8, even for very simple layouts. After lots of experimentation, I (mostly) went back to doing manual layout and manual calculation of the cell's height.

    Update 2: I've run some tests on iOS 9 and it seems that UITableViewAutomaticDimension finally works as advertised. Yay!

提交回复
热议问题