1

What I'm trying to do is create a view controller, and have its view animate in from the right. The main view controller has a view called viewContainer that will house this view. The weird thing is that this works works perfectly in the simulator and on an ipod 4g, but does not work on the iPhone5 (both running ios6).

On the ipod 4g, the new view correctly animates in from the right. On the iPhone5 however, the view appears immediately at (0,0). What could be causing this?

self.catViewController = [[CatViewController alloc] initWithNibName:@"CatViewController" bundle:nil];
[self.catViewController willMoveToParentViewController:nil];

[self addChildViewController:self.catViewController];
self.currentViewController = self.catViewController;

[self showNextViewController];

- (void) showNextViewController
{

    NSLog(@"should be at x: %f", self.viewContainer.bounds.size.width); //This correctly logs 320 as the starting x position of the new view

    //set the new view's position off screen to the right
    self.currentViewController.view.frame = CGRectMake(self.viewContainer.bounds.size.width, self.viewContainer.bounds.origin.y, self.currentViewController.view.frame.size.width, self.currentViewController.view.frame.size.height);
    [self.viewContainer addSubview:self.currentViewController.view];

    //inexplicably to me, this logs 0.0 instead of 320 on the iphone5:
    DLog(@"actual starting x position: %f", self.currentViewController.view.frame.origin.x);


    [UIView animateWithDuration:kDefaultAnimationTime
                          delay:0.0f
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{self.currentViewController.view.frame = self.viewContainer.bounds;}
                     completion:^(BOOL finished){
                         //
                     }];
}
soleil
  • 12,133
  • 33
  • 112
  • 183
  • Where is all of this taking place in your view lifecycle? Things likeNavigation Controllers and Tab Bars don't get sized until ViewWillAppear. If you're trying to do calculations on Frames and Bounds in ViewDidLoad, you may get sporadic results. – Axeva May 09 '13 at 15:47
  • This happens when a buttons is pushed on the home view controller. – soleil May 09 '13 at 15:48
  • Is the child view controller the same size as the parent? If so, set the CGRectMake parameters based on the parent's dimensions, not the child's. – Axeva May 09 '13 at 15:50
  • The child view controller's view is the same size as self.viewContainer. The problem isn't the size of the child view controller's view though, it's the position of it. – soleil May 09 '13 at 15:54
  • Does setting the frame of self.currentViewController.view AFTER calling addSubview make any difference? – Mike Pollard May 09 '13 at 16:00
  • @MikePollard yes it does actually. Any idea why? And why is the behavior different on the iphone5? I'll mark your answer correct if you want to post as an answer. – soleil May 09 '13 at 16:05
  • No idea :), it was just a hunch that it must have been the addSubview call that was changing the frame for some reason. Are the iPod and iPhone 5 running _exactly_ the same version of iOS? You could always subclass UIView, make self.currentViewController.view an instance of your subclass and breakpoint in setFrame: to see what's going on? – Mike Pollard May 10 '13 at 09:59

0 Answers0