I have heard that viewDidLayoutSubviews
is the best place to alter the layout when we have used constraints.
So I jumped to viewDidLayoutSubviews
I have created three UIViews
and SubViewed
them on SuperView. I am not mentioning the code for frame calculation.
[self.view addSubview:imgMiddleCircle];
[self.view addSubview:imgFirstCircle];
[self.view addSubview:imgLastCircle];
Using this piece of I am adding these circles.
Now when I run my code In viewDidLayoutSubviews
I get following screens:
And when I switch to viewWillLayoutSubviews
I am getting this on screen:
Why I am getting extra two circles in viewDidLayoutSubviews
even I am creating three circles.
And why in viewWillLayout
gives the correct Output.
You should code for the fact that viewDidLayoutSubviews
is called multiple types.
Ideally addSubview:
should be happening in a place like viewDidLoad
where you are sure it is only called once.
You can create a flag to avoid calling addSubview:
multiples types (not my personal choice)
Otherwise, try to move your set up code to viewDidLoad
and force the view to render itself before doing your calculation:
[yourView setNeedsLayout];
[yourView layoutIfNeeded];