在iOS 6中无法处理方向?

20
我正在使用代理方法- (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;

}

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

输入图片描述

7个回答

46

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

[self.window addSubView:viewControllerObj]

以上行存在问题。在iOS 5中,以上代码可以使方向正常工作,但在iOS上,为了使方向正常工作,请使用以下代码替换以上代码:

[self.window setRootViewController:viewControllerObj]

然后应用程序将在方向更改时进行旋转。


你是怎么知道的?这解决了我的问题。+5 - Janak Nirmal
+1,部分解决了我的问题,在http://stackoverflow.com/questions/13704218/ios6-autorotation-portaitupsidedown-to-portait。我无法正确处理倒置的情况。 - user529543
1
只有最顶层的控制器会收到方向变化的通知。如果将控制器添加为子视图,则不会将消息传递给该控制器。 - Dennis Stritzke

3

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

此外,您在shouldAutorotateToInterfaceOrientation:中的代码可以放在viewDidLayoutSubviews中。


我已经添加了那些设置。即使如此,当我改变设备方向时,屏幕方向仍然没有改变。为什么?我已经上传了info.plist文件的部分截图,请查看... - Bharath
2
你的答案部分正确。但是,我另一个错误是将视图控制器添加为窗口的子视图。相反,如果我将该视图控制器对象设置为[self.window setRootViewController:viewControllerObj],则方向就可以正常工作! - Bharath
啊,很好的发现。视图控制器和窗口存在问题,只有根视图控制器才能接收方向变更通知。 - Léo Natan

1

请记住,在iOS 6中,旋转的处理是在父视图中进行的。子视图控制器的责任较少。但对于那些没有使用界面构建器编写所有代码的人来说,这更加烦人。


6
苹果在iOS 6的方向性问题上表现很差。我们必须改变所有应用程序以支持iOS 6。(为什么不支持旧的方式呢) - Bagusflyer
完全同意。最糟糕的是我改变了所有支持iOS6的内容,并删除了旧方法,所以当我在一段时间后测试旧版iPad时,什么都不起作用了。 - lagos

0

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

0

在iOS6中使用willAnimateRotationToInterfaceOrientation


0

这种方法适用于IOS 6及更早版本

-(BOOL)rotationChanged:(UIInterfaceOrientation)interfaceOrientation { NSInteger orientation = [[UIDevice currentDevice] orientation]; UIWindow *_window = [[[UIApplication sharedApplication] delegate] window]; if ([PGPlatformUtils GetCurrentPlatform]==PGPlatformEnum_iPhone) { switch (orientation) { case 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 animated:YES];
[UIView commitAnimations]; break; case 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 animated:YES];
[UIView commitAnimations]; break; case 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 animated:YES];
[UIView commitAnimations]; break; case 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 animated:YES];
[UIView commitAnimations]; break; case 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 animated:YES];
[UIView commitAnimations]; break;
default: break; } } else{ switch (orientation) { case 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 animated:YES]; NSLog(@"*** 1 Orientation Call 0");
[UIView commitAnimations]; break; case 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 animated:YES]; NSLog(@"*** 1 Orientation Call M_PI"); [UIView commitAnimations]; break; case 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 animated:YES]; NSLog(@"*** 1 Orientation Call M_PI/2"); [UIView commitAnimations]; break; case 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 animated:YES]; NSLog(@"*** 1 Orientation Call - M_PI/2"); [UIView commitAnimations]; break; case 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 animated:YES]; NSLog(@"*** 1 Orientation Call 0"); [UIView commitAnimations]; break;
default: break; } } return YES;
}

0

请确保您的所有方向都已启用。在此输入图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接