objective c - Set Autolayout Constraints Relative to Device Size -
i fell trap of relying on autolayout. have viewcontroller ton of uiviews , realized they're distorted on smaller devices (iphone 4s , 5).
is possible set constraints in autolayout relative device size.
for example, have 1 uiview
several other uiviews
aligned edges. change height in autolayout of 1 uiview equal half device height.
yes. there way constrain screen size.
they called size classes.
in interface builder on bar @ bottom of screen says wany hany
, pulldowns. select combination of height , width of device , orientation want constrain to, , when create constraints in mode, they'll specific size/orientation. can add , modify size-specific constraints in inspector, constraint editor pane, in interface builder.
if need tweak corner case things aren't able accomplish conveniently enough size clases, can make iboutlet constraint , refer in code, , modify when view appears , changes, similar following example. easier , safer trying generate constraints scratch programmatically.
note: when modifying nslayout constraint via ib outlet, can tweak constant
field, not multiplier
because multiplier
readonly @ runtime. whatever scaling factor use (if any) must multiplied whatever final value use constant.
@iboutlet var tableytopconstraint : nslayoutconstraint! override func viewwillappear(animated: bool) { adjustviewlayout(uiscreen.mainscreen().bounds.size) } override func viewwilltransitiontosize(size: cgsize, withtransitioncoordinator coordinator: uiviewcontrollertransitioncoordinator) { adjustviewlayout(size) } func adjustviewlayout(size: cgsize) { switch(size.width, size.height) { case (320, 480): // iphone 4s in portrait tableytopconstraint.constant = 0 case (480, 320): // iphone 4s in landscape tableytopconstraint.constant = 0 case (320, 568): // iphone 5/5s in portrait tableytopconstraint.constant = 0 case (568, 320): // iphone 5/5s in landscape tableytopconstraint.constant = 0 case (375, 667): // iphone 6 in portrait tableytopconstraint.constant = 0 case (667, 375): // iphone 6 in landscape tableytopconstraint.constant = 0 case (414, 736): // iphone 6 plus in portrait tableytopconstraint.constant = 0 case (736, 414): // iphone 6 plus in landscape tableytopconstraint.constant = 0 default: break } view.setneedslayout() }
Comments
Post a Comment