Re: Animating autolayout constraint changes for subviews
Re: Animating autolayout constraint changes for subviews
- Subject: Re: Animating autolayout constraint changes for subviews
- From: Steve Christensen <email@hidden>
- Date: Wed, 28 Dec 2016 22:19:19 -0800
Sorry, I mostly copied your code and moved things around. Try -setNeedsLayout instead of -layoutIfNeeded.
> On Dec 28, 2016, at 9:58 PM, Doug Hill <email@hidden> wrote:
>
> Hello Steve,
>
> FWIW, I’ve tried both ways and it doesn’t seem to affect the problem I’m having. However, Apple says to update constraints than do the animation block with layoutIfNeeded, according to this WWDC session:
>
> https://developer.apple.com/videos/play/wwdc2012/228/?id=228 <https://developer.apple.com/devcenter/download.action?path=/videos/wwdc_2012__sd/session_228__best_practices_for_mastering_auto_layout.mov>
>
> But in general, the SDK documentation on animating autolayout constraint changes is borderline non-existent.
>
> Doug Hill
>
>
>> On Dec 28, 2016, at 5:54 PM, Steve Christensen <email@hidden <mailto:email@hidden>> wrote:
>>
>> I have always put the thing that I'm animating into the animation block:
>>
>> - (IBAction)animateIt:(id)sender
>> {
>> static BOOL small = NO;
>>
>> small = !small;
>>
>> CGFloat newWidth = small ? self.view.frame.size.width / 2 : self.view.frame.size.width;
>>
>> [UIView animateWithDuration:1 animations:
>> ^{
>> self.containerWidthConstraint.constant = newWidth;
>> [self.view layoutIfNeeded];
>> }];
>> }
>>
>>> On Dec 28, 2016, at 4:14 PM, Doug Hill <email@hidden <mailto:email@hidden>> wrote:
>>>
>>> Hi Ken,
>>>
>>> I uploaded a sample project here:
>>>
>>> https://github.com/djfitz/TestAutolayoutAnimations <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