I have many places in my code where I need to do stuff based upon the layout of the views in a UIViewController
. For example, I have a view in a nib that I then capture in code and use to create a mask for another layer, but I need to wait until the mask view and its subviews have the correct size before I do so.
I have discovered that, although hacky, I can achieve this by creating a counter which increments each time viewDidLayoutSubviews
is called, and triggers the code I wish to execute when it has been called for the second time - for some reason, most of the views have yet to properly lay themselves out until viewDidLayoutSubviews
is called for the second time.
Like I say, this seems pretty hacky and there's every chance that an iOS update could break my code. Does anyone have a better way of doing this?
- (void)viewDidLoad
{
[super viewDidLoad];
self.layoutCount = 0;
}
- (void)viewDidLayoutSubviews
{
[super viewDidLayoutSubviews];
self.layoutCount ++;
if (self.layoutCount == 2)
{
// perform final layout code, e.g. create masks
}
}