I have an app with a ViewController that contains a headerView, the headerView is just a container which should hold other views. The views inside the headerView are the views I want to replace at runtime with an animation. I want the oldView (which is the old content of the headerView) to slide out to the left and the newView to slide in from the right. I've included the code below and the log. The slide in animation of the newView works, but the oldView always slides to the (0,0) coordinates, instead of the (-headerView.bounds.size.width, 0) coordinates. Is this kind of animation impossible, or isn't the UIView animateWithDuration made for this kind of animations? Thanks a lot already.
The code:
//put the frame at the right side of out bounds (to slide in)
[newView setFrame:CGRectMake(newView.frame.origin.x + newView.frame.size.width, newView.frame.origin.y, newView.frame.size.width, newView.frame.size.height)];
NSLog(@"Oldview bounds before transition: %f - %f - %f - %f", oldView.frame.origin.x, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);
[UIView animateWithDuration:1.0 animations:^{
//put it in the correct position (slide in from right to left)
[newView setFrame:CGRectMake(0, 0, self.headerView.frame.size.width, self.headerView.frame.size.height)];
//Slide the old view out of the view to the left
[oldView setFrame:CGRectMake(-self.headerView.bounds.size.width, 0, self.headerView.frame.size.width, self.headerView.frame.size.height)];
NSLog(@"Oldview bounds after transition: %f - %f - %f - %f", oldView.frame.origin.x, oldView.frame.origin.y, oldView.frame.size.width, oldView.frame.size.height);
} completion:^(BOOL fin){
//remove the old view from the headerview
[oldView removeFromSuperview];
}];
Log output:
Oldview bounds before transition: 0.000000 - 0.000000 - 760.000000 - 400.000000
Oldview bounds after transition: -760.000000 - 0.000000 - 760.000000 - 400.000000
[SOLUTION] My oldView contained constraints which kept the view in position, by setting:
oldView.translatesAutoresizingMaskIntoConstraints = YES;
Before the animation solved the problem.