SnapKit icon indicating copy to clipboard operation
SnapKit copied to clipboard

Initial setup constraints code shouldn't be written in "updateConstraints" method

Open Huanhoo opened this issue 6 years ago • 1 comments

Hi,

According to the Apple Developer Document

In general, you can make these changes at any time. Ideally, most constraints should be set up in Interface Builder, or programatically created by the view controller during the controller’s initial setup (for example, in viewDidLoad). If you need to dynamically change constraints at runtime, it’s usually best to change them when the application’s state changes. For example, if you want to change a constraint in response to a button tap, make that change directly in the button’s action method.

Apple's recommended most constraints should be set up in Interface Builder or during the controller’s initial setup (for example, in viewDidLoad).

But you suggest writing the "adding" code in the updateConstraints method which will add (include initial setup constraints) constraints to UIView

image

Incorrect comments will lead developer to write inappropriate code. I have found that many developers will write the initial setup constraint code in "updateConstraints" method and said that this is what the Snapkit team suggested doing this.

So your document should be modified?

Note: I also opened the same issue in "Masonry".

Huanhoo avatar Apr 18 '19 02:04 Huanhoo

https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/AutolayoutPG/ModifyingConstraints.html

Update Pass

The system traverses the view hierarchy and calls the updateViewConstraints method on all view controllers, and the updateConstraints method on all views. You can override these methods to optimize changes to your constraints (see Batching Changes).

I agree, don’t use updateConstraints() for the initial setup of your view.

cntrump avatar Jun 01 '21 16:06 cntrump