Reputation: 920
I have a subclass of UIView, I want to manipulate a constraint but it does not work.
When pressing a button exchangesViewActivated changes and the functions get called.
var exchangesViewActivated = false {
didSet {
if exchangesViewActivated == true {
setExchangesView()
} else {
setUpLayout()
}
}
}
Die subview and translatesAutoresizingMaskIntoConstraints are set.
func setUpLayout() {
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = true
bottomContainer.leadingAnchor.constraint(equalTo: scrollViewContrainer.leadingAnchor).isActive = true
bottomContainer.trailingAnchor.constraint(equalTo: scrollViewContrainer.trailingAnchor).isActive = true
bottomContainer.topAnchor.constraint(equalTo: configBar.bottomAnchor).isActive = true
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = true
}
Now I want to manipulate the constraint by calling this function:
func setExchangesView() {
bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor).isActive = false
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive = false
bottomContainer.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: 0).isActive = true
}
But this constraint stays activated:
bottomContainer.heightAnchor.constraint(equalToConstant: 500).isActive
Am I missing something? Is it not enough to set a constraint to false to deactivate it? Do I have to call something else?
Upvotes: 0
Views: 987
Reputation: 19156
setUpLayout()
is adding new constraints every time you call setUpLayout()
method. You must save constraint reference and update it next time.
For example.
// class variable
var bottomConstraint:NSLayoutConstraint? = nil
bottomConstraint = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
bottomConstraint.isActive = true
And later update constraint
bottomConstraint.constant = 100
bottomConstraint.isActive = true
Upvotes: 2
Reputation: 100541
This doesn't deactivate old constraints as it deactivates the newly created ones so do
var heightCon:NSLayoutConstraint!
var botCon:NSLayoutConstraint!
//
heightCon = bottomContainer.heightAnchor.constraint(equalToConstant: 500)
heightCon.isActive = true
botCon = bottomContainer.bottomAnchor.constraint(equalTo: scrollViewContrainer.bottomAnchor)
botCon.isActive = true
Then you can easily reference the constraints and deactivates them and add the new constraints
Upvotes: 1