Eric
Eric

Reputation: 109

How to set constraint in a reusable UITableViewCell

My xib layout likes under drawing I have set the constraints from "a" to "b" to ..."e" besides the height constant of "c" so that can determine the height of the cell. Sometimes the "c" will hidden because some reasons, I changed the height constant to zero and actual height by the label's content when it is visible.

--------------------------------------
UILabel(name-singleline)         a
UILabel(title-Multi line)         b
UIView(hidden or not)             c (default height constant = 80)
 -UILabel(content Multi line)     d
UILabel(singleline)               e
--------------------------------------

But there is a autolayout warning make me crazy

(
    "<NSLayoutConstraint:0x15756760 V:[UILabel:0x15756650'XXXX'(15)]>",
    "<NSLayoutConstraint:0x15757130 V:[UIView:0x15756ee0(80)]>",
    "<NSLayoutConstraint:0x15758060 V:|-(21)-[UILabel:0x15756650'XXXX']   (Names: '|':UITableViewCellContentView:0x157563a0 )>",
    "<NSLayoutConstraint:0x15758120 V:[UILabel:0x15756650'XXXX']-(14)-[Project.GWWLabel:0x157573d0'YYYY']>",
    "<NSLayoutConstraint:0x157581e0 V:[Project.GWWLabel:0x157573d0'YYYY']-(3)-[UIView:0x15756ee0]>",
    "<NSLayoutConstraint:0x15758210 V:[UIView:0x15756ee0]-(8)-[UILabel:0x157569e0'ZZZZ']>",
    "<NSLayoutConstraint:0x15758270 V:[UILabel:0x157569e0'ZZZZ']-(18)-|   (Names: '|':UITableViewCellContentView:0x157563a0 )>",
    "<NSLayoutConstraint:0x1575d760 'UIView-Encapsulated-Layout-Height' V:[UITableViewCellContentView:0x157563a0(139.5)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15757130 V:[UIView:0x15756ee0(80)]>

I'm also deeply interested in understanding why this happens

Upvotes: 2

Views: 823

Answers (2)

APesate
APesate

Reputation: 100

The first golden rule of auto layout (specially when working on TableView's cells) is to be able to draw a line from top to bottom and from left to right and be able to determine the size of each element. To manage this you will need to be sure that each view has at least one constraint per side (unless it size could be inferred)

Also sometimes when testing the UI on different screen size the constraints might break. In does cases you should change the priority of the constraints or content hugging / compression resistance of the views to allow auto layout to break does constraints and change the constants to adapt the screen size. (Most of the cases it will only change in a few points, if it changes more you might need to change your constraints)

Is also important to mention that "Hidden views, even though they don't draw, still participate in Auto Layout and usually retain their frames, leaving other related views in their places." check this post

You may want to check this post or this one if you use swift

Upvotes: 0

Pritesh
Pritesh

Reputation: 980

Try this:

make the priority of height constrain 750 as shown below it will work when you change height constant

enter image description here

Upvotes: 3

Related Questions