Evgeniy Kleban
Evgeniy Kleban

Reputation: 6940

Changing NSLayoutConstraint

I want to change width of scroll view. First, i did this:

var scrollWidthConstraint: NSLayoutConstraint!

Then set it:

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

Then i want to change it like:

  scrollWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: width)
        scrollWidthConstraint.isActive = true
        contentView.setNeedsLayout()

However it appears that i did not remove old constraint, i see in log:

"<NSLayoutConstraint:0x6000031d7160 UIView:0x7fc92c808120.width == 1333   (active)>",
    "<NSLayoutConstraint:0x6000031fcff0 UIView:0x7fc92c808120.width == 933.1   (active)>"

How to remove old constraint and update it?

Upvotes: 0

Views: 83

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

This

scrollWidthConstraint = contentView.widthAnchor.constraint(equalToConstant: width)
    scrollWidthConstraint.isActive = true
    contentView.setNeedsLayout()

creates another constraint regardless off you re-assign it to the old one , you need

scrollWidthConstraint.constant = width
view.layoutIfNeeded()

Upvotes: 2

Related Questions