了解iOS 6界面方向更改

Sta*_*tas 23 iphone objective-c uiinterfaceorientation ios6

补充: 我看到我的问题经常在没有赞成的情况下被查看,所以我决定你们没有得到你搜索的内容.重定向到有关如何处理iOS6中的方向更改的答案非常好的问题

方向变化的具体要求: 限制轮换

欢迎Upvotes :)


我已经从Master Detail模板创建了一个新项目,并尝试以横向方向启动它.如你所知

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

方法已弃用,我们必须使用

- (NSUInteger)supportedInterfaceOrientations

和/或

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

这是我的代码:

- (NSUInteger)supportedInterfaceOrientations {
    NSLog(@"supported called");
    return UIInterfaceOrientationMaskAll;//Which is actually a default value
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    NSLog(@" preferred called");//This method is never called. WHY?
    return UIInterfaceOrientationLandscapeRight;
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的那样,我试图在首选方法中返回横向,但它永远不会被调用.ps文档说明:

讨论系统在全屏显示视图控制器时调用此方法.当视图控制器支持两个或更多方向时,您可以实现此方法,但内容在其中一个方向中最佳.

如果视图控制器实现此方法,则在显示时,其视图将以首选方向显示(尽管稍后可以将其旋转到另一个受支持的旋转).如果未实现此方法,系统将使用状态栏的当前方向显示视图控制器.

所以,问题是:为什么永远不会调用prefferredOrientation方法?我们应该如何在不同的控制器中处理不同的方向?谢谢!PS不会将问题标记为重复.我调查了所有类似的问题,他们没有我的答案.

mat*_*att 34

关于preferredInterfaceOrientationForPresentation

preferredInterfaceOrientationForPresentation永远不会被调用,因为这不是"呈现"的视图控制器.这里没有涉及"演示".

"呈现"和"呈现"不是一些模糊的术语,意思是"出现".这些是精确的技术术语,意味着该视图控制器与呼叫一起发挥作用presentViewController:animated:completion:.换句话说,只有当我们以前称之为"模态"视图控制器时,才会到达此事件.

那么,你的视图控制器不是模态视图控制器; 它没有发挥作用presentViewController:animated:completion:.所以没有涉及"演示",因此preferredInterfaceOrientationForPresentation在这里无关紧要.

我对此非常明确,因为我认为很多人会像你一样被混淆或误导.所以也许这个笔记会帮助他们.

启动进入景观

在iOS 6中,Info.plist中的"支持的接口方向"键比以前更加重视.启动到所需方向的整体问题的解决方案是:

  1. 确保"支持的界面面向"在Info.plist中列出的所有方位的应用程序将永远被允许承担.

  2. 确保所需的启动方向首先在"支持的接口方向"中.

这里的所有都是它的.实际上,您不应该将任何代码放入根视图控制器来管理初始方向.


Fun*_*Kat 8

如果您想在横向模式下启动模态视图,只需将此代码放在显示的视图控制器中即可

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    UIInterfaceOrientation orient = [[UIApplication sharedApplication] statusBarOrientation];
    if (UIInterfaceOrientationIsLandscape(orient)) {
        return orient;
    }

    return UIInterfaceOrientationLandscapeLeft;
}
Run Code Online (Sandbox Code Playgroud)

然后,像往常一样出示这个控制器

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.navigationController presentViewController:vc animated:YES completion:^{}];
Run Code Online (Sandbox Code Playgroud)