I've just asked a question about changing a UIView height with proportional constraint. The problem was that I should have been using activate and deactivate for the constraints.
This works fine once, but I want to toggle between two constraints.
I have a UIView called topView and two constraints:
@IBOutlet var cHtTopView1: NSLayoutConstraint!
@IBOutlet var cHtTopView2: NSLayoutConstraint!
The first one scales the UIView to 0.3 times the height of the superview, the second to 0.5. This works once:
@IBAction func buttonTouchUpInside(_ sender: Any) {
NSLayoutConstraint.deactivate([self.cHtTopView1])
NSLayoutConstraint.activate([self.cHtTopView2])
}
However, if I have another button that tries to activate cHtTopView1 it gives an error. I'm pretty sure this is because the constraint is deleted from code. So, I then tried this:
@IBAction func buttonTouchUpInside(_ sender: Any) {
self.topView.removeConstraint(self.cHtTopView1)
self.topView.addConstraint(self.cHtTopView2)
}
This gives an error at .addConstraint. Not sure why this is - any help greatly appreciated. Thanks, Ian