简体   繁体   中英

simple programmatic modification of a single IB constraint not updating

EDIT: It seems i need to change the multiplier and not the constant.

I'm trying to programmatically change the width of a button outside of viewDidLoad (if that matters) after a certain event has taken place.

I've set the buttons width in IB to equal the Views width.

I've created a IB outlet for the buttons width constraint:

@IBOutlet weak var imageButtonWidth: NSLayoutConstraint!

Then I've tried:

 imageButtonWidth.constant = 0.49
    view.setNeedsLayout()

,

 imageButtonWidth.constant = 0.49
    view.layoutIfNeeded()

and

imageButtonWidth.constant = 0.49
    self.view.updateConstraints()

I've set a breakpoint at imageButtonWidth.constant = 0.49 and it crashes the app so I know its being "ran" it's just not activating correctly.

I've used

imageButtonWidth.constant = 0.49
    self.view.updateConstraints()

in viewdidload on a different application with no issue, but it seems if the call is outside viewdidload it won't be acknowledged.

any advice would be greatly appreciated!

The right way to do it is with

 self.view.layoutIfNeeded()

to update the constraint but you probably connected a wrong outlet it seems since the value of the constraint seems to low.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM