2

My situation is very similar to this question . I have an app that is universal where iPhone is in portrait orientation and the iPad is in Landscape and I switch between all of my main screens using the appDelegate.window.rootViewController = newScreen; The iPhone app works perfectly. The iPad app will sometime throw the screen up in portrait orientation instead of landscape. Here is some sample transition code:

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
            ViewController* v = nil;
            if (IS_IPAD()) {
                v = [[ViewController alloc] initWithNibName:@"ViewController-iPad" bundle:nil];
            }else{
                v = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            }
            [appDelegate.window setRootViewController:(UIViewController*)v];

I also tried this from this other question :

AppDelegate* appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
            ViewController* v = nil;
            if (IS_IPAD()) {
                v = [[ViewController alloc] initWithNibName:@"ViewController-iPad" bundle:nil];
            }else{
                v = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
            }

            [UIView
             transitionWithView:appDelegate.window 
             duration:0.5
             options:UIViewAnimationOptionTransitionCrossDissolve
             animations:^(void) {
                 BOOL oldState = [UIView areAnimationsEnabled];
                 [UIView setAnimationsEnabled:NO];
                 [appDelegate.window setRootViewController:(UIViewController*)v];
                 [UIView setAnimationsEnabled:oldState];
             } 
             completion:nil];

But nothing has fixed it.

Also tried using [appDelegate.window makeKeyAndVisable] and [appDelegate.window makeKeyWindow] in the hopes that this would "shock" it into doing the correct orientation.

Community
  • 1
  • 1
Matthew Clark
  • 571
  • 1
  • 9
  • 33
  • 1
    This is a peculiar way to switch view controllers, which I've never seen before, but it may work. Did you add an appropriate `-(BOOL)shouldAutorotateToInterfaceOrientation:` to your view controllers? That should be the place where the interface orientation is determined. – mvds Jun 26 '12 at 23:08
  • I did indeed. Looks like this: if (IS_IPAD()) { return UIInterfaceOrientationIsLandscape(interfaceOrientation); } return (interfaceOrientation == UIInterfaceOrientationPortrait); – Matthew Clark Jun 27 '12 at 00:33
  • Hmm. You could try to switch to a less rigorous way of switching VCs, e.g. by using a fixed (blank) view controller, and presenting your various views modally on them. This might keep the interface orientation fixed more reliably. – mvds Jun 27 '12 at 00:53
  • this is a full app that I'm just adding iPad on top of... if I can fix it in a less invasive way I would rather, but I'll keep that in mind. Thanks^^ – Matthew Clark Jun 27 '12 at 00:58
  • Hey Matthew, did you find an answer to your question? – Joseph Humfrey Jul 09 '12 at 21:39

1 Answers1

4

I've got it!

Here's what my transition block looks like. The key thing I did was copy the transform (the thing that does the actual rotation) from the CALayer. Note that you'll have to include the Quartz framework and #import <QuartzCore/QuartzCore.h> at the top of your file.

[UIView transitionWithView:self.window 
                  duration:0.5 
                   options:UIViewAnimationOptionTransitionCrossDissolve 
                animations:^{                        
    BOOL oldState = [UIView areAnimationsEnabled];
    [UIView setAnimationsEnabled:NO];
    target.view.layer.transform = window.rootViewController.view.layer.transform;
    window.rootViewController = target;
    [UIView setAnimationsEnabled:oldState];
} completion:nil];
Joseph Humfrey
  • 2,974
  • 2
  • 23
  • 34