9

I am trying to set up the width of a UIButton using this code:

constraintButtonPlayWidth = NSLayoutConstraint(item: buttonPlay,
        attribute: NSLayoutAttribute.Width,
        relatedBy: NSLayoutRelation.Equal,
        toItem: self.view,
        attribute: NSLayoutAttribute.Width,
        multiplier: 1,
        constant: 100)
self.view.addConstraint(constraintButtonPlayWidth)

But the button gets stretched too much; probably because toItem: self.view. I tried modifying the constant of the constraint, but that didn't change anything.

How do I set up correctly this constraint so it actually has a width of 100?

1 Answer 1

28

You were close. The constraint only should have a single item since it is not relative to another item.

constraintButtonPlayWidth = NSLayoutConstraint (item: buttonPlay,
    attribute: NSLayoutAttribute.Width,
    relatedBy: NSLayoutRelation.Equal,
    toItem: nil,
    attribute: NSLayoutAttribute.NotAnAttribute,
    multiplier: 1,
    constant: 100)
self.view.addConstraint(constraintButtonPlayWidth)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.