Is it possible to set the Frame before ViewDidLoad() in a UIViewController?
Currently, I do this in the constructor:
View.Frame = _frame;
The problem is that this call automatically triggers first the ViewDidLoad() function.
In ViewDidLoad() I place my subviews dependant on the “View.Frame” property.
But this property only changes after ViewDidLoad() was completed.
So basically “View.Frame = _frame;” first trigger “ViewDidLoad()” with the old “Frame” value and only after that the property is changed.
I can solve this by first assigning a class variable to “_frame” and use that in “ViewDidLoad()”, but I would like to see if there is another solution and if I’m missing something here.
(I’m also missing the idea behind this flow)
This is a common situation on iOS, and I would say you are doing the right thing for your constructor. Calling the getter on the
Viewproperty will load the view, so they provide anIsViewLoadedproperty to check for this.If it is not a constructor where you are passing in the frame, you could also do something like this in your controller:
Because of this scenario, it might be worth thinking about your design. I don’t know the full situation, but could your controller decide its
Framewithin the class? Maybe you could pass another value in that would let you do the math forFrameinViewDidLoad, and that would match Apple’s pattern a little better.