0

I set up width of UIView like this:

contentView.widthAnchor.constraint(equalToConstant: CGFloat(scrollWidth())).isActive = true

Now I want to change that width. The question is: how to remove old constraint and add new? I did create NSLayoutConstraint like this

scrollWidthConstraint = NSLayoutConstraint(
    item: contentView,
    attribute: .width,
    relatedBy: .equal,
    toItem: nil,
    attribute: .notAnAttribute,
    multiplier: 1.0,
    constant: CGFloat(scrollWidth())
)

How to set that constraint for UIView for changing later?

1
  • for your case you don't have to remove old constraint and add another Commented Mar 20, 2019 at 13:38

1 Answer 1

3

First use this better and short and assign a reference to it

var widCon:NSLayoutConstraint! 

widCon = contentView.widthAnchor.constraint(equalToConstant: CGFloat(scrollWidth()))
widCon.isActive = true

instead of NSLayoutConstraint(item: contentView, then Change it's constant

widCon.constant = ////
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.