ios - How to add views in an existing view made in Interface Builder with Auto Layout? -


i have created following auto layout in interface builder:

enter image description here

as can see didn't give fix size buttons. add 2 button programmatically result:

enter image description here

adding constraints programmatically know how that, @ least know syntax.

my problem when create buttons?

i create width constraint based on width of button 4. if in viewdidload (if i'm not wrong), auto layout hasn't been set yet width (and height) wrong.

i thought in viewdidlayoutsubviews it's called multiple times when loading viewcontroller, multiple buttons stacked on each other , when go landscape more buttons added..

when should create button have right sizes?

auto layout rules hold @ times, not (primarily) frame sizes @ 1 moment.

you should not care getting frame of button 4 when set constraints buttons 5 , 6. constraint add buttons 5 , 6 should refer button 4's width attribute, not current width in points. is, create constraint this:

nslayoutconstraint* constraint = [nslayoutconstraint constraintwithitem:button5 attribute:nslayoutattributewidth relatedby:nslayoutrelationequal toitem:button4 attribute:nslayoutattributewidth multiplier:1 constant:0]; constraint.active = yes; // or: [button5.superview addconstraint:constraint] 

that's constraint keep button 5's width same button 4's width, button 4's width changes. same height, , button 6. etc.

put way, constraints create @ runtime should similar create in ib if doing @ design time. doesn't me you've created explicit, fixed height , width constraints on button 4. you've created relative constraints relating height , width other views.

one thing have do: since buttons 2 , 4 have trailing space constraints container (or margins), need remove constraints when add buttons 5 , 6. buttons 2 , 4 have have trailing constraints buttons 5 , 6, respectively, , buttons 5 , 6 have have trailing constraints container. actually, should simplify getting rid of button 4's trailing constraint container , replacing trailing alignment constraint button 2. likewise, button 6's trailing edge should aligned button 5's, not spaced superview's. way, have remove 1 constraint (button 2's trailing superview) , add 1 (button 5's trailing superview).


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -