【问题标题】:rootViewController set but comes up as incorrect orientationrootViewController 设置但出现方向不正确
【发布时间】:2012-06-28 08:22:15
【问题描述】:

我的情况与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],希望这会“震撼”它做出正确的方向。

【问题讨论】:

  • 这是一种切换视图控制器的特殊方式,我以前从未见过,但它可能会起作用。您是否在视图控制器中添加了适当的-(BOOL)shouldAutorotateToInterfaceOrientation:?那应该是确定界面方向的地方。
  • 我确实做到了。看起来像这样: if (IS_IPAD()) { return UIInterfaceOrientationIsLandscape(interfaceOrientation); } return (interfaceOrientation == UIInterfaceOrientationPortrait);
  • 嗯。您可以尝试切换到不太严格的方式来切换 VC,例如通过使用固定(空白)视图控制器,并以模态方式呈现您的各种视图。这可能会更可靠地保持界面方向固定。
  • 这是一个完整的应用程序,我只是在上面添加了 iPad……如果我能以一种侵入性较小的方式修复它,我更愿意,但我会记住这一点。谢谢^^
  • 嗨,马修,您找到问题的答案了吗?

标签: ios nib uiinterfaceorientation uiapplicationdelegate


【解决方案1】:

我明白了!

这是我的过渡块的样子。我做的关键是从 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];

【讨论】:

  • 出色的修复,非常感谢!
猜你喜欢
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
相关资源
最近更新 更多