Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RootViewController animation transition, initial orientation is wrong

So I followed this thread: RootViewController Switch Transition Animation to transit the window.rootViewController from A to B to C. Code looks like this:

[UIView transitionWithView:self.window 
                  duration:0.5 
                   options: UIViewAnimationOptionTransitionFlipFromLeft 
                animations:^{
                               self.window.rootViewController = newViewController;
                } 
                completion:nil];

The problem is my app shall only support landscape, but during the rootViewController transition, the new view controller appears in portrait mode than quickly rotate to landscape mode.

I'm sure that:

  1. I've set UISupportedOrientation to landscape (home button right)
  2. for each viewcontroller, in the shouldAutoRotateToOrientation method, I set only for landscape

What could be the other reason?

like image 752
Chris Chen Avatar asked Nov 08 '11 16:11

Chris Chen


2 Answers

I looked into this just now because I kept getting the same issue. I randomly tried the following, and it worked perfectly:

[UIView
    transitionWithView:window 
    duration:0.5
    options:UIViewAnimationOptionTransitionCrossDissolve
    animations:^(void) {
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        [(ICApp *)sharedApplication.delegate window].rootViewController = self;
        [UIView setAnimationsEnabled:oldState];
    } 
    completion:nil];

I know it's a bit odd to disable/enable animations inside an animation block, but the cross dissolve animates, and the rotation does not -- the view controller appears already rotated and ready to roll.

like image 153
Kalle Avatar answered Nov 18 '22 20:11

Kalle


Just put in another animation option UIViewAnimationOptionAllowAnimatedContent:

[UIView transitionWithView:self.window duration:0.5 options:(UIViewAnimationOptionTransitionFlipFromLeft | UIViewAnimationOptionAllowAnimatedContent) animations:^{
    self.window.rootViewController = newViewController;
} completion:nil];
like image 9
Borut Tomazin Avatar answered Nov 18 '22 21:11

Borut Tomazin