简体   繁体   English

了解iOS 6界面方向更改

[英]Understanding iOS 6 Interface orientation change

ADDED: I see that my question is viewed often without upvotes so I decided that you guys do not get what you search. 补充:我看到我的问题经常在没有赞成的情况下被查看,因此我认为你们没有得到你搜索到的东西。 Redirecting you to question that has really nice answer about How to handle orientation changes in iOS6 重定向您的问题,对于如何处理iOS6中的方向更改有非常好的答案

Specific demands to orientation changes: Restricted rotation 方向变化的具体要求: 限制轮换

Upvotes are welcome :) 欢迎Upvotes :)


I've created a new project from Master Detail template and trying to start it with landscape orientation. 我已经从Master Detail模板创建了一个新项目,并尝试以横向方向启动它。 As you know the 如你所知

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

method is deprecated and we must use 方法已弃用,我们必须使用

- (NSUInteger)supportedInterfaceOrientations

and/or 和/或

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

Here's my code: 这是我的代码:

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

As you can see I'm trying to return landscape orientation in preferred method but it is never called. 正如你所看到的那样,我试图在首选方法中返回横向,但它永远不会被调用。 ps documentation states: ps文档说明:

Discussion The system calls this method when presenting the view controller full screen. 讨论系统在全屏显示视图控制器时调用此方法。 You implement this method when your view controller supports two or more orientations but the content appears best in one of those orientations. 当视图控制器支持两个或更多方向时,您可以实现此方法,但内容在其中一个方向中最佳。

If your view controller implements this method, then when presented, its view is shown in the preferred orientation (although it can later be rotated to another supported rotation). 如果视图控制器实现此方法,则在显示时,其视图将以首选方向显示(尽管稍后可以将其旋转到另一个受支持的旋转)。 If you do not implement this method, the system presents the view controller using the current orientation of the status bar. 如果未实现此方法,系统将使用状态栏的当前方向显示视图控制器。

So, the question is: Why the prefferredOrientation method is never get called? 所以,问题是:为什么永远不会调用prefferredOrientation方法? And how should we handle different orientations in different controllers?. 我们应该如何在不同的控制器中处理不同的方向? Thanks! 谢谢! PS don't mark the question as duplicate. PS不会将问题标记为重复。 I've investigated all similar questions and they do not have answer for mine. 我调查了所有类似的问题,他们没有我的答案。

About preferredInterfaceOrientationForPresentation 关于preferredInterfaceOrientationForPresentation

preferredInterfaceOrientationForPresentation is never called because this is not a "presented" view controller. preferredInterfaceOrientationForPresentation永远不会被调用,因为它不是“呈现的”视图控制器。 There is no "presentation" involved here. 这里没有涉及“演示”。

"Presented" and "presentation" are not some vague terms meaning "appears". “呈现”和“呈现”不是一些模糊的术语,意思是“出现”。 These are precise, technical terms meaning that this view controller is brought into play with the call presentViewController:animated:completion: . 这些是精确的技术术语,意味着这个视图控制器与调用presentViewController:animated:completion:一起发挥presentViewController:animated:completion: . In other words, this event arrives only if this is what we used to call a "modal" view controller. 换句话说,只有当我们以前称之为“模态”视图控制器时,才会到达此事件。

Well, your view controller is not a modal view controller; 那么,你的视图控制器不是模态视图控制器; it is not brought into play with presentViewController:animated:completion: . 没有presentViewController:animated:completion:发挥presentViewController:animated:completion: . So there is no "presentation" involved, and therefore preferredInterfaceOrientationForPresentation is irrelevant here. 所以没有涉及“演示”,因此preferredInterfaceOrientationForPresentation在这里是无关紧要的。

I'm being very explicit about this because I'm thinking that many folks will be confused or misled in the same way you were. 我对此非常明确,因为我认为很多人会像你一样被混淆或误导。 So perhaps this note will help them. 所以也许这个笔记会帮助他们。

Launch into Landscape 启动进入景观

In iOS 6, the "Supported Interface Orientations" key in your Info.plist is taken much more seriously than previously. 在iOS 6中,Info.plist中的“支持的接口方向”键比以前更加严肃。 The solution to your overall problem of launching into a desired orientation is: 启动到所需方向的整体问题的解决方案是:

  1. Make sure that "Supported Interface Orientations" in your Info.plist lists all orientations your app will ever be allowed to assume. 确保“支持的界面面向”在Info.plist中列出了所有的方位您的应用程序将永远不会允许承担。

  2. Make sure that the desired launch orientation is first within the "Supported Interface Orientations". 确保所需的启动方向首先在“支持的接口方向”中。

That's all there is to it. 这里的所有都是它的。 You should not in fact have to put any code into the root view controller to manage the initial orientation. 实际上,您不应该将任何代码放入根视图控制器来管理初始方向。

If you would like launch modal view in Landscape Mode, just put this code in presented view controller 如果您想在横向模式下启动模态视图,只需将此代码放在显示的视图控制器中即可

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

    return UIInterfaceOrientationLandscapeLeft;
}

Then, present this controller as usual 然后,像往常一样出示这个控制器

UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self.navigationController presentViewController:vc animated:YES completion:^{}];

There is a very simple answer: You can only change or fix the interface orientation of a modal presented view controller. 有一个非常简单的答案:您只能更改或修复模态呈现视图控制器的界面方向。 If you do so ie with a Present Modally segue in Interface builder (or the navigation controller method) you can define the allowed orientations with 如果您这样做,即在“界面”构建器(或导航控制器方法)中使用“当前模态偏移”,则可以定义允许的方向

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait; // for example else an addition of all allowed
}

This event doesn't fire up when you only push a view controller on the navigation Controller ... so : You don't have a BACK button and need a 只按下导航控制器上的视图控制器时,此事件不会启动...所以:您没有BACK按钮,需要一个

[self dismissViewControllerAnimated:YES completion: ^(void) {

    }];

to close it. 关闭它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM