Search code examples
iossnapkitmasonry-ios-osx

The right way to write masonry code? in “- init” or “- layoutSubViews”


Both ways seem to work, are there any differences in performance?


Solution

  • Well it really depends on what you want to do. You can call it from init but then the code will only be called when you instantiate the class. If you put it in layoutSubviews you can have the code called multiple times by making the view redraw.

    So if you need to update constraints based on things changing it could be advantageous to use layoutSubviews but if you don't need to modify the constraints I would personally make a new method called setConstraints which would contain all the constraints and call that from init