Is calling updateViewConstraints manually a good idea?

橙三吉。 提交于 2020-01-02 23:13:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!