xcode iOS6 中的纵向和横向模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12728390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:42:35  来源:igfitidea点击:

Portrait and Landscape mode in iOS6

xcodeios6landscape-portrait

提问by Lars -

When updating my app to iOS6 standard the portrait / landscape is gone. Ir worked perfectly when I was building with Xcode 3. But now using latest Xcode and latest SDK the rotation is gone and it is always in portrait mode. No matter what I put in "Supported interface Orientations". And the code I used to get rotation before seems to have no effect at all. I had these lines.

将我的应用程序更新到 iOS6 标准时,纵向/横向消失了。当我使用 Xcode 3 构建时,Ir 工作得很好。但是现在使用最新的 Xcode 和最新的 SDK 旋转消失了,它始终处于纵向模式。无论我在“支持的界面方向”中输入什么。而我之前用来获取旋转的代码似乎根本没有效果。我有这些线。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:
            return YES;
        default:
            return NO;
    }
}

How do I change and what do I change to get it work again?

我该如何改变,我需要改变什么才能让它再次发挥作用?

回答by mayuur

First of all, in AppDelegate, write this. THIS IS VERY IMP

首先,在AppDelegate中,写下这个。这是非常Imp

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
     return (UIInterfaceOrientationMaskAll);
}

Then, For UIViewControllers, in which you need only PORTRAIT mode, write these functions

然后,对于只需要 PORTRAIT 模式的 UIViewControllers,编写这些函数

- (BOOL)shouldAutorotate
{
     return YES;
}

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

For UIViewControllers, which require LANDSCAPE too, change masking to All.

对于也需要 LANDSCAPE 的 UIViewControllers,将掩码更改为 All。

- (NSUInteger)supportedInterfaceOrientations
{
    return (UIInterfaceOrientationMaskAllButUpsideDown);
    //OR return (UIInterfaceOrientationMaskAll);
}

Now, if you want to do some changes when Orientation changes, then use this function.

现在,如果您想在 Orientation 更改时进行一些更改,请使用此功能。

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{

}

EDIT :

编辑 :

A lot depends on with which controller is your UIViewController embedded in.

很大程度上取决于您的 UIViewController 嵌入在哪个控制器中。

Eg, If its inside UINavigationController, then you might need to subclass that UINavigationController to override orientation methods like this.

例如,如果它在 UINavigationController 内部,那么您可能需要子类化该 UINavigationController 以覆盖这样的方向方法。

subclassed UINavigationController (the top viewcontroller of the hierarchy will take control of the orientation.) did set it as self.window.rootViewController.

子类 UINavigationController(层次结构的顶部视图控制器将控制方向。)确实将其设置为 self.window.rootViewController。

 - (BOOL)shouldAutorotate
 {
     return self.topViewController.shouldAutorotate;
 }
 - (NSUInteger)supportedInterfaceOrientations
 {
     return self.topViewController.supportedInterfaceOrientations;
 }

From iOS 6, it is given that UINavigationController won't ask its UIVIewControllers for orientation support. Hence we would need to subclass it.

从 iOS 6 开始,UINavigationController 不会要求其 UIVIewControllers 提供方向支持。因此我们需要对它进行子类化。

回答by Hox

How to support one or more landscape controllers in app that is portrait mainly in ios6:

如何在 ios6 中以竖屏为主的 app 支持一个或多个横屏控制器:

1) in AppDelegate

1) 在 AppDelegate 中

 - (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
    {
        UINavigationController* ns = (UINavigationController*)self.window.rootViewController;
        if (ns) {
            UIViewController* vc = [ns visibleViewController];
//by this UIViewController that needs landscape is identified
            if ([vc respondsToSelector:@selector(needIos6Landscape)])
                return [vc supportedInterfaceOrientations];

        }
        return UIInterfaceOrientationMaskPortrait; //return default value
    }

2) in UIView controller(s) that needs landscape (or portrait+lanscape etc):

2)在需要横向(或纵向+横向等)的 UIView 控制器中:

//flag method
-(void)needIos6Landscape {
}
- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

3) in controllers, to which you can RETURN from controllers, that can be rotated in landscape - this is important, otherwise they remaind landscape on return from landscape-enabled VC.

3)在控制器中,您可以从控制器返回,可以横向旋转 - 这很重要,否则它们在从启用横向的 VC 返回时保持横向。

- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

4) (maybe not needed, but for sure..) - subclass navigation controller(s) you using, and add:

4)(可能不需要,但可以肯定......) - 你使用的子类导航控制器,并添加:

- (BOOL)shouldAutorotate
{
    return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController* vc = [self visibleViewController];
    if (vc) {
        if ([vc respondsToSelector:@selector(needIos6Landscape)]) {
             return [vc supportedInterfaceOrientations];
        }
    }
    return UIInterfaceOrientationMaskPortrait;
}

The important step is to ask for orientation only controllers from your app, because during transition between controllers, for some time there is some system controller as root, and will return incorrect value (this took me 2 hrs to find out, it was reason it was not working).

重要的一步是从您的应用程序中要求仅定向控制器,因为在控制器之间转换期间,有一段时间有一些系统控制器作为 root,并且会返回不正确的值(这花了我 2 小时才找到,这是原因没有工作)。

回答by Koen

Don't know whether your issue was alike but with me, the status bar was oriented correctly (landscape) and the UIViewControllerwas portrayed. I changed following line in the application delegate application:didFinishLaunchingWithOptions:

不知道您的问题是否相似,但对我来说,状态栏的方向正确(横向)并且UIViewController被描绘出来。我在应用程序委托application 中更改了以下行:didFinishLaunchingWithOptions:

//[window addSubview:navigationController.view];

self.window.rootViewController = navigationController;

Apple=> this costed me a day and a half to find out, and a lot of money!!!

Apple=> 这花了我一天半的时间才弄明白,而且花了很多钱!!!