Reputation: 2923
I need your help to make this:
I have a view which width is equal to the width of screen. In this view I have 2 subviews with width 1/2 of parent view and aspect 1:1, so the height of the parent view should be equal to the height of subviews.
After that I need to perform animation which should add to the parent view another view and transform a whole screen like this:
Should I use autolayout programmatically to achieve this? How to get this behaviour easier?
Upvotes: 2
Views: 1819
Reputation: 2312
The easiest way would be.
Implement "viewDidLayoutSubviews" method for the view controller as below.
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
}
Inside this method, find the current width of the parent view, and store it in a variable "X".
When the view is loaded for first time, set the width constraint's constant as x/2 for first and second view.
When 3rd view is ready to display, set width constraint's constant as x/3 for all the three view inside an animation block.
Upvotes: 1
Reputation: 2881
You can...
First, instead of making them half the parent view, I would:
Then when you add the new view,
Then call
[UIView animateWithDuration:0.5 animations:^{
[view layoutIfNeeded];
}];
Note that if the default is just two views, you could add the first set of constraints via storyboard (which is easier than doing it programmatically). The rest of it you can do programmatically: See Apple documentation.
Personally, I like this method, because you don't have to do any math on the size or positions of the views - autolayout will figure it out for you.
Upvotes: 2