I have separate arrays of NSLayoutConstraints
built using visual formatting strings that I want to use for portrait and landscape orientations. I have tried two methods for switching between the two: activating/deactivating them, and adding/removing them.
Activating/Deactivating:
portaitConstraints.forEach {
$0.active = false
}
landscapeConstraints.forEach {
$0.active = true
}
Adding/Removing:
self.view.removeConstraints(portraitConstraints)
self.view.addConstraints(landscapeConstraints)
Both methods seem to work fine and behave as expected, but I get the following runtime error with the activate/deactivate method:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it.
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints)
Is there a common pattern for this? Can I safely ignore the error if it behaves as expected? Is there more overhead for one method versus the other? I thought it made more sense to just flip the "active" property on the ones I wanted active versus repeatedly adding and removing constraints to/from the view.
Or is this just a dumb way to do this, and I need to do something like change the NSLayoutAttributes
directly?