2

I am trying to animate a full-screen view sliding in from the left and using the whole space.

I have the following code which does the expected job perfectly fine on iOS 8, but fails to do so on iOS 7 :

[source.view addSubview:destination.view];

NSDictionary *viewsDict = @{@"tableView":destination.tableView,
                            @"tapView": destination.tapView,
                            @"destinationView": destination.view};

[source.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[destinationView]|" options:0 metrics:nil views:viewsDict]];

source.leftConstraint = [NSLayoutConstraint constraintWithItem:destination.tableView
                                         attribute:NSLayoutAttributeLeft
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:source.view
                                         attribute:NSLayoutAttributeLeft
                                        multiplier:1.0
                                          constant:-source.view.bounds.size.width];

[source.view addConstraint:source.leftConstraint];

source.rightConstraint = [NSLayoutConstraint constraintWithItem:source.view
                                         attribute:NSLayoutAttributeTrailing
                                         relatedBy:NSLayoutRelationEqual
                                            toItem:destination.tapView
                                         attribute:NSLayoutAttributeTrailing
                                        multiplier:1.0
                                          constant:source.view.bounds.size.width];

[source.view addConstraint:source.rightConstraint];

[source.view layoutIfNeeded];

[UIView animateWithDuration:.35f animations:^{

    source.rightConstraint.constant = 0;
    source.leftConstraint.constant = 0;

    [source.view layoutIfNeeded];

}];

When this gets executed on iOS 7 the destination.view gets added to the screen straight away without any animation. Any idea why that might be the case ?

Petar
  • 2,241
  • 1
  • 24
  • 38
  • Did you resolve this? Seeing the same issue. – tagy22 Mar 09 '15 at 09:45
  • I changed my code a little bit and I am having a consistent behaviour with iOS 7 and iOS 8 now, but still this code does behave differently. – Petar Mar 09 '15 at 11:25

1 Answers1

0

Your animation block should only contain [source.view layoutIfNeeded]; (see How do I animate constraint changes?).

You should change the constraints before the block, like this :

[source.view layoutIfNeeded];

source.rightConstraint.constant = 0;
source.leftConstraint.constant = 0;

[UIView animateWithDuration:.35f animations:^{

    [source.view layoutIfNeeded];

}];
Community
  • 1
  • 1
Maxime LM
  • 61
  • 4
  • @session 232 from 2012 wwdc at 56min explain that you need to set starting constraints, then call `layoutIfNeeded`, then in the animation block - change the constants of your constraints and call `layoutIfNeeded` again ? This is the source I have followed for my code. – Petar Nov 14 '14 at 14:31
  • The mechanics may have changed since that. I use the above code in my app and it's working in both iOS 7&8. Maybe you could try it and see if it works ? – Maxime LM Nov 14 '14 at 14:37