rootViewController设置但出现方向不正确

时间:2012-06-26 22:59:32

标签: ios nib uiinterfaceorientation uiapplicationdelegate

我的情况与this question非常相似。我有一个通用的应用程序,其中iPhone处于纵向方向,iPad处于横向状态,我使用appDelegate.window.rootViewController = newScreen在我的所有主屏幕之间切换; iPhone应用程序完美运行。 iPad应用程序有时会以纵向而非横向方式投放屏幕。以下是一些示例转换代码:

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];

我也是从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];

但没有任何东西可以修复它。

还尝试使用[appDelegate.window makeKeyAndVisable]和[appDelegate.window makeKeyWindow],希望这会“震惊”它以正确的方向。

1 个答案:

答案 0 :(得分:4)

我知道了!

这是我的转换块的样子。我做的关键是从CALayer复制变换(实际旋转的东西)。请注意,您必须在文件顶部包含Quartz框架和#import <QuartzCore/QuartzCore.h>

[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];