At what point are UIViewController subroutines definitely initially laid out?

I have many places in my code where I need to do things based on the layout of the views in UIViewController

. For example, I have an encoded view that I then code into and use to create a mask for another layer, but I need to wait until the mask view and its subtypes are the correct size before I do this.

I found that while a hack, I can achieve this by creating a counter that increments each time it is called viewDidLayoutSubviews

and runs the code I want to execute the second time it is called - for whatever reason, most views still do not develop themselves until viewDidLayoutSubviews

they are called a second time.

As I said, this seems to be quite a hack, and there is every chance that an iOS update could break my code. Does anyone have a better way to do 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
    }
}

      

+3


source to share


1 answer


The safest approach is to "update" the masks every time it is called viewDidLayoutSubviews

. this way you don't have to worry about whether the views were laid out or not.



+1


source







All Articles