First, I should mention, this is mostly an efficiency issue.
There are many discussions as to where to do frame calculations where viewWillAppear
is too early and viewDidAppear
is too late (view is already visible).
The common answer is to do frame calculations in viewDidLayoutSubviews
. Problem is, it gets called multiple times. Worse, the most accurate call, the one where all frames have their final size is the last one. To my knowledge there is no way to know which call is that final one.
We use to have a 'framesAreSet' flag (initialized false) and a check, where if frame is not zero (something like self.view.frame.size.width != 0
) and 'framesAreSet' is false, it goes in, turn the flag and calculate only once. Something like:
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
if (self.view.frame.size.width != 0 && !framesAreSet)
{
framesAreSet = true;
//Calculate frames here
}
}
This looks ok, but in truth, a check for something like self.view.frame.size.width != 0
does not guarantee that frames are indeed set. The fact that viewDidLayoutSubviews
will get called after suggests that some frames were not set to their final state.
Would be great to have a viewCompleteLayoutSubviews
. Any ideas of what's the best way to accomplish a 'one time' frame calculations when all frames are set and view is not yet visible?
(This is probably an issue with views not using NSConstraints)