【问题标题】:Unable to handle orientation in iOS 6?无法在 iOS 6 中处理方向?
【发布时间】:2012-09-20 19:35:37
【问题描述】:

我正在使用

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

委托根据方向类型更改我的视图框架

即,

if(UIInterfaceOrientationIsLandscape(interfaceOrientation))
{
    self.view.frame=CGRectMake(0,0,500,300);
}
else
{
    self.view.frame=CGRectMake(0,0,300,400);
}

如何在 iOS 6 中处理与

相同的情况
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

在 iOS6 中已被弃用。

我正在使用以下委托来设置所有方向。

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationAllMask;
}

但是,

-(BOOL)shouldAutoRotate
{
    return YES;

}

没有被调用。这种情况如何处理?

【问题讨论】:

    标签: ipad uiinterfaceorientation ios6 autoresizingmask iphone-5


    【解决方案1】:

    在 AppDelegate 中,我已将 ViewController 对象添加到窗口中

    [self.window addSubView:viewControllerObj]

    问题出在上面一行。在 iOS 5 中,方向将与上述行正常工作,但在 iOS 中,要使方向正常工作,请使用

    更改上述行

    [self.window setRootViewController:viewControllerObj]

    然后当方向改变时应用会旋转。

    【讨论】:

    • 你是怎么知道的?这解决了我的问题。 +5
    • +1,在stackoverflow.com/questions/13704218/…部分解决了我的问题我无法正确处理倒置的情况
    • 只有最上面的控制器会收到方向变化的通知。如果您将控制器添加为子视图,则消息不会传递给此控制器。
    【解决方案2】:

    确保项目和目标中的设置允许每种设备类型的方向。

    另外,您在shouldAutorotateToInterfaceOrientation: 中的代码可以输入viewDidLayoutSubviews

    【讨论】:

    • 我也添加了该设置。即使那样,当我更改设备方向时,方向也不会改变。为什么?我已经上传了 info.plist 文件的部分屏幕截图。请看..
    • 您的回答部分正确。但是,我犯的另一个错误是我将 viewController 作为子视图添加到窗口中。相反,如果我将该 viewController 对象设置为[self.window setRootViewController:viewControllerObj],则方向工作正常!
    • 啊,不错。视图控制器和窗口存在问题,只有根视图控制器接收方向更改通知。
    【解决方案3】:

    请记住,在 iOS 6 中,旋转的处理是在父视图中小心处理的。对子视图控制器的责任较小。但对我们来说更烦人的是没有接口生成器的代码。

    【讨论】:

    • Apple 在 iOS 6 的定位方面做得非常糟糕。我们必须更改所有应用程序以支持 iOS 6。(为什么它也不支持旧方式)
    • 完全同意。最糟糕的是我更改了所有内容以支持 iOS6,并删除了旧方法,所以当我在旧 iPad 上测试一段时间后,没有任何效果。
    【解决方案4】:

    确保所有方向都已启用。

    【讨论】:

      【解决方案5】:

      在 iOS 6 中处理 UINavigation 方向问题

      1 创建一个 UINavigation+Rotation 类别类

      2 在 UINavigation+Rotation.m 类中放入以下方法

      -(BOOL)shouldAutorotate
      {
          return [[self.viewControllers lastObject] shouldAutorotate];
      }
      
      -(NSUInteger)supportedInterfaceOrientations
      {
          return [[self.viewControllers lastObject]supportedInterfaceOrientations];
      }
      
      - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
      {
          if ([self.viewControllers count] == 0) {
              return UIInterfaceOrientationPortrait;
          }
          return [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
      }
      

      【讨论】:

        【解决方案6】:

        此方法适用于 IOS 6 及旧版本

        -(BOOL)rotationChanged:(UIInterfaceOrientation)interfaceOrientation { NSInteger 方向 = [[UIDevice currentDevice] 方向]; UIWindow *_window = [[[UIApplication sharedApplication] delegate] window]; if ([PGPlatformUtils GetCurrentPlatform]==PGPlatformEnum_iPhone) { 开关(方向){ 情况1: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (0)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait 动画:YES]; [UIView 提交动画]; 休息; 案例2: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (M_PI)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait 动画:YES]; [UIView 提交动画]; 休息; 案例3: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight 动画:YES]; [UIView 提交动画]; 休息; 案例4: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft 动画:YES]; [UIView 提交动画]; 休息; 案例5: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (0)]; [_window setFrame:CGRectMake(0, 0, 320, 480)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIPrintInfoOrientationLandscape 动画:YES]; [UIView 提交动画]; 休息; 默认: 休息; } } 别的{ 开关(方向){ 情况1: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (0)]; [_window setFrame:CGRectMake(0, 0, 768, 1024)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait 动画:YES]; NSLog(@"*** 1 定向调用 0"); [UIView 提交动画]; 休息; 案例2: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (M_PI)]; [_window setFrame:CGRectMake(0, 0, 768, 1024)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortraitUpsideDown 动画:YES]; NSLog(@"*** 1 定向调用 M_PI"); [UIView 提交动画]; 休息; 案例3: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 768, 1024)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight 动画:YES]; NSLog(@"*** 1 次定向调用 M_PI/2"); [UIView 提交动画]; 休息; 案例4: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (- M_PI / 2)]; [_window setFrame:CGRectMake(0, 0, 768, 1024)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft 动画:YES]; NSLog(@"*** 1 定向调用 - M_PI/2"); [UIView 提交动画]; 休息; 案例5: [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.0]; [_window setTransform:CGAffineTransformMakeRotation (0)]; [_window setFrame:CGRectMake(0, 0, 768, 1024)]; [[UIApplication sharedApplication] setStatusBarOrientation:UIPrintInfoOrientationLandscape 动画:YES]; NSLog(@"*** 1 定向调用 0"); [UIView 提交动画]; 休息; 默认: 休息; } } 返回是; }

        【讨论】:

          【解决方案7】:

          在 iOS6 中使用willAnimateRotationToInterfaceOrientation

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-16
            • 2013-09-09
            • 1970-01-01
            • 2012-09-30
            • 1970-01-01
            相关资源
            最近更新 更多