iOS 6旋转问题-呈现模态视图控制器时没有旋转

16

我有一个MainViewController,它有一个按钮,通过水平翻转的方式推出一个新视图(InfoViewController),如下所示:

controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];

主视图控制器支持纵向和倒立的纵向。如下所示:

- (BOOL)shouldAutorotate {
    return YES;    
}

- (NSUInteger)supportedInterfaceOrientations {    
    return (UIInterfaceOrientationMaskPortrait | 
            UIInterfaceOrientationMaskPortraitUpsideDown);
}

在我的InfoViewController中,也标明了上述代码。在我的AppDelegate中,LaunchOptions有这个:

[self.window setRootViewController:self.mainViewController];

我的app.plist文件支持所有方向,因为其他视图也需要支持横屏方向。所以在我的MainViewController和InfoViewController中,我只需要竖屏和倒立竖屏。但是在另一个视图中,我需要所有方向。

我的MainViewController工作正常,但是我的InfoViewController适用于所有方向。

我尝试在iOS6中实现这个功能非常困难。我已经研究了其他帖子,并尝试了其他人提供的方法,但完全没有成功。请有人帮助我实现这个,谢谢。我是Objective-C新手:p


标题中写着“无旋转”。在倒数第二段中,它说它适用于所有方向。那到底是哪一个? - Mundi
4个回答

26

在您的应用程序plist文件中不支持所有方向,只支持根视图控制器支持的方向。

iOS 6中旋转方向发生了变化。在iOS 6中,UIViewController的shouldAutorotateToInterfaceOrientation:方法已被弃用。取而代之的是,您应该使用supportedInterfaceOrientationsForWindow:shouldAutorotate方法:

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;    
}

iOS 6中,全屏展示的Modal ViewControllers不再接收旋转消息: 对于任何以全屏方式呈现自身的视图控制器(例如使用presentViewController:animated:completion:调用的视图控制器), willRotateToInterfaceOrientation:duration:willAnimateRotationToInterfaceOrientation:duration:didRotateFromInterfaceOrientation:方法都不再被调用。

你可以让呈现你的Modal View Controllers的视图控制器通知它进行旋转。 同时,现在你应该使用presentViewController:animated:completion:来呈现视图控制器, 而presentModalViewController:animated:已经过时,你在代码中使用了这个方法。


该视图的 shouldAutorotate 返回 YES。 - ryryan
然后您返回支持的接口方向,以允许旋转。 - Sverrisson
7
在iOS 6中,模态视图控制器不再接收旋转调用。 - Sverrisson
看起来我完蛋了。 - ryryan
让我们在聊天中继续这个讨论:http://chat.stackoverflow.com/rooms/17035/discussion-between-hart1994-and-hannes-sverrisson - ryryan
显示剩余5条评论

3

我曾经解决过类似的问题,当时使用了选项卡控制器。

子类化UITabBarController,并实现以下方法:

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}


- (BOOL)shouldAutorotate
{
    NSLog(@"Orientation:%d", [[UIDevice currentDevice] orientation]);

    for (UIViewController *viewController in self.viewControllers) {
        [viewController shouldAutorotate];
    }

    return YES;
}

如果您想在标签栏控制器内处理控制器的旋转,那么在标签栏控制器中的每个控制器中也要实现这些方法,并编写代码来处理方向更改。如果不想处理,则不需要实现这些方法。当方向更改时,TabBarControllers方法始终会运行。甚至由于未知原因可能会运行两次。
是的,别忘了删除所有shouldAutorotate方法。我完全转到了新的方向模型。如果你想让它们保持不变,可能会更难。

0
将以下代码添加到UITabBarController的子类.m文件中。
@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}
@end

@implementation NameClassUITabBar

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}

@end

我在选项卡控制器中处理旋转的解决方案/经验:

http://luterr.blogspot.sg/2015/04/example-code-uiinterfaceorientationmask.html

链接已失效(博客已被删除)。 - Peter Mortensen

0

通过子类化UINavigationController来创建一个类别,并在.h文件中实现以下方法

-(BOOL)shouldAutorotate;
-(NSUInteger)supportedInterfaceOrientations;
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation;



 in .m file

-(BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}

-(NSUInteger)supportedInterfaceOrientations
 {
return [self.topViewController supportedInterfaceOrientations];
 }

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return [self.topViewController preferredInterfaceOrientationForPresentation];
 } 

在视图控制器类中实现以下方法,以启用旋转的类

-(NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |   UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
 }


- (BOOL)shouldAutorotate
{
return YES;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
   return UIInterfaceOrientationLandscapeLeft;
}

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