I ran into a problem that suggests I may be implementing a design pattern improperly. I have dynamic subviews whose sizes are language dependent. When I present a modal view, I set the layout during viewDidLoad. I noticed that a UILabel object was not dispalying correctly and found the problem to be that the size of the modal view during viewDidLoad is not the same as the size when the view is shown.
The width of the modal view during viewDidLoad s 768. After the view has appeared, the width is 540.
This suggests that I should be setting layout during viewWillAppear, and not during viewDidLoad.
This seems to contradict my understanding of the DRY principle, if I apply it to execution, as the layout isn’t going to change once the view is loaded, so I would be unnecessarily repeating the layout code every time the view appears. (I know that in my example, the language setting could be changed in between presentations of the modal, but there may be other examples where this type of dynamic doesn’t exist.)
I implement the layout in non-modal views the same way — in viewDidLoad.
Should I be doing this differently?
Assuming a view won’t change is incorrect. For example, when changing orientation. Usually setting your view in viewDidLoad, in conjunction with correct auto resizing mask, is sufficient in most cases. If you need finer control of setting up when the view resizes, you should override viewWillLayoutSubviews/viewDidLayoutSubviews, and rearrange your views there. So in viewDidLoad you would add your subviews and et their content, then in layout set their sizes and positions.