问题
I'm creating my constraints mainly in viewDidLoad
. In updateViewConstraints
I'm adding or removing some subviews and additionally adding and removing constraints. As a side note I'm using a container with child view controllers and I'm reusing the same view for different orientations (full in landscape, in a popover in portrait).
Because I have my complete setup in updateViewConstraints
I have to call it in some situations manually to adapt the visual changes and not to brake the constraints (e.g. when presenting the same view in a popover, or on iOS 7 it is not called on rotation). I also tried to use setNeedsUpdateConstraints
and so on but that always broke my constraints.
My question now is am I allowed to call updateViewConstraints
manually? Does it has some negative side effects? Or is the flow of my application wrong?
回答1:
According to Erica Sadun in the excellent iOS Auto Layout Demystified, 2nd Edition, it's perfectly acceptable to call updateViewConstraints
directly:
When working with views, you call setNeedsUpdateConstraints (setNeedsUpdate Constraints: on OS X) to indicate that a view needs attention at the next layout pass. With view controllers, you call the updateViewConstraints method directly, generally when setting up (viewWillAppear:) and responding to rotation callbacks.
来源:https://stackoverflow.com/questions/26625706/is-calling-updateviewconstraints-manually-a-good-idea