swift - Trouble adjusting constraints programmatically -
hey have setup this:
the left constraint constant 111. want change constraint 20 , increase length of box. have:
@ibaction func didtapbutton(sender: anyobject) { view.removeconstraint(leadingconstraint) leadingconstraint = nslayoutconstraint(item: view.superview!, attribute: .leading, relatedby: .equal, toitem: greenview, attribute: .leading, multiplier: 1.0, constant:20) view.addconstraint(leadingconstraint) }
i had error leadingconstraint = nslayoutconstraint(item: view.superview!, attribute: .leading, relatedby: .equal, toitem: greenview, attribute: .trailing, multiplier: 1.0, constant:20)
. changed .leading , .leading both attributes. rectangle stretching it's not correct size, it's going off screen:
what doing wrong here? (note: want change these constraints adding new constraint not changing constant value. i'm trying better understanding!). pointers on appreciated! thanks
the way should is:
storyboard:
- create 2 constraints (one first state , 1 end one).
- change priority of each constraint. first 1 must higher second, not 1000. let's 751 first , 749 second.
- create iboutlet each constraint.
code:
- change priority of each constraint (firstconstraint.priority = 749, secondconstraint.priority = 751).
- do view.layoutifneeded().
- all these must done in main thread.
hope works.
Comments
Post a Comment