Re: Animating autolayout constraint changes for subviews
Re: Animating autolayout constraint changes for subviews
- Subject: Re: Animating autolayout constraint changes for subviews
- From: Doug Hill <email@hidden>
- Date: Wed, 28 Dec 2016 16:14:51 -0800
Hi Ken,
I uploaded a sample project here:
https://github.com/djfitz/TestAutolayoutAnimations
I tried to strip this down to what is needed to show the behavior I see.
My code to actually do the animation is this:
- (IBAction)animateIt:(id)sender
{
static BOOL small = NO;
if( small )
{
[self.view layoutIfNeeded];
self.containerWidthConstraint.constant = self.view.frame.size.width;
[UIView animateWithDuration:1 animations:
^{
[self.view layoutIfNeeded];
}];
}
else
{
[self.view layoutIfNeeded];
self.containerWidthConstraint.constant = self.view.frame.size.width / 2;
[UIView animateWithDuration:1 animations:
^{
[self.view layoutIfNeeded];
}];
}
small = !small;
}
'container view' has one subview which is a UILabel. The label is pinned to the superview edges via autolayout constraints. (e.g. trailing, leading, top, bottom edges all pinned to superview edges.)
I tried a few different variations, including leaving out the first layoutIfNeeded (which some people say should be done, others not).
The exact behavior is that the label will resize to the new size immediately and reflow the text, then the container view will animate it's size change. It would be nice if both the label and the container view animate at the same time.
Also, as I mentioned, a button will exhibit the same behavior, probably because it has a UILabel inside it to show the button text.
Thanks again for any ideas.
Doug Hill
> On Dec 28, 2016, at 12:50 PM, Ken Thomases <email@hidden> wrote:
>
> On Dec 28, 2016, at 1:55 PM, Doug Hill <email@hidden> wrote:
>>
>> I can now animate my constraint changes but I notice that subviews aren't animated. For example, I have a single view with a width constraint, and this view has a label as a subview that expands to the size of it's parent view via edge constraints.
>> I can change the width constraint constant of the parent view at runtime and it animates very well. However, the subviews jump into place immediately then the parent view animates into place. I see the same behavior with a button as a subview.
>
> Show exactly how you're animating the constraint changes. Are you really animating the change of the constraint or are you doing a layoutIfNeeded within an animation context? Even if the former, are you calling any methods that force layout (layoutIfNeeded or similar)? If so, where/when?
>
> Regards,
> Ken
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden