UITableViewCell auto height based on amount of UILabel text

前端 未结 5 1425
终归单人心
终归单人心 2020-12-03 20:54

I have a bit of a tricky set up in my storyboard, I have a UIViewController that holds a UITableViewController. Within the UITableViewController I have several prototypecell

5条回答
  •  盖世英雄少女心
    2020-12-03 21:54

    To go a little further on Dao's answer..

    He is correct, you need to use UITableViewAutomaticDimension

    Also, you need to ensure that you setup your constraints in a way that all of the content in the cell is constrained to the cells contentView. So your label will likely need constraints such as

    • Leading constraint to ImageView
    • Top constraint to contentView
    • Bottom constraint to contentView
    • Trailing constraint to contentView

    Make sure that you set the UILabel to multiline (or lines = 0) and it should work.

    If you are using the heightForRowAt delegate functions ensure you return UITableViewAutomaticDimension

提交回复
热议问题