xcode 为什么 iPhone 5 不旋转倒置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12542472/
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:34:18  来源:igfitidea点击:

why iPhone 5 doesn't rotate to upsideDown

xcoderotationios6

提问by Jeanette Müller

i added the new methods to my code like described at apples iOS 6 Documentations but on the iPhone 5 the App doesn't rotate to upside down. Only the to landscape Ways.

我将新方法添加到我的代码中,如苹果 iOS 6 文档中所述,但在 iPhone 5 上,该应用程序不会倒置。只有到景观的方式。

Here my Code from the rootViewController:

这是我来自 rootViewController 的代码:

- (NSUInteger)supportedInterfaceOrientations{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
            UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
- (BOOL)shouldAutorotate{
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return YES;
}

i tryed also "UIInterfaceOrientationMaskAll" but no changes. The Strange is, my iPhone 4 with iOS6 does totate o upside down with the same code.

我也试过“UIInterfaceOrientationMaskAll”但没有变化。奇怪的是,我的 iPhone 4 和 iOS6 确实用相同的代码颠倒了。

Any Ideas?

有任何想法吗?

回答by Cliff Ribaudo

IOS 6 uses default Orientation UIInterfaceOrientationMaskAllfor iPad and UIInterfaceOrientationMaskAllButUpsideDownfor iPhone. If you want your ViewControllers on the iPhone to rotate to ALL orientations including UpsideDown you must add the iOS 6 method:

IOS 6 使用UIInterfaceOrientationMaskAlliPad 和UIInterfaceOrientationMaskAllButUpsideDowniPhone 的默认方向。如果您希望 iPhone 上的 ViewControllers 旋转到所有方向,包括 UpsideDown,您必须添加 iOS 6 方法:

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

to all view controllers. If you've done this and it is not working then I am betting your using a TabViewController or NavigationController where some of the views are NOT returning this. All ViewControllers must return this for ANY of them to support it.

到所有视图控制器。如果你已经这样做了并且它不起作用那么我打赌你使用的是 TabViewController 或 NavigationController ,其中一些视图没有返回这个。所有的 ViewControllers 都必须返回它来支持它们中的任何一个。

Alternatively you can add a Category to the UITabViewController or NavigationController class that is your rootViewController and override that method. You do that like so:

或者,您可以向作为您的 rootViewController 的 UITabViewController 或 NavigationController 类添加一个 Category 并覆盖该方法。你这样做:

@interface UITabBarController (RotationAll)
    -(NSUInteger)supportedInterfaceOrientations;
@end

@implementation UITabBarController (RotationAll)
-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}
@end

Also these are the currently recommended approaches now being discussed in Apple Developer Forums with Apple staff saying earlier documentation that discouraged sub classing of UINavigationController is out of date, and as of iOS 6 it is ok to do that.

此外,这些是目前在 Apple 开发者论坛中正在讨论的当前推荐方法,Apple 工作人员说早期的文档不鼓励 UINavigationController 的子类化已经过时,从 iOS 6 开始可以这样做。

回答by Chandan

Thanks to Cliff Ribaudo really help full your answer for UITabbarController based project but in my case i am only using UINavigationController so i have modified your answer for UINavigationController categeory.

感谢 Cliff Ribaudo 确实帮助您完成了基于 UITabbarController 的项目的答案,但在我的情况下,我只使用 UINavigationController,因此我修改了您对 UINavigationController 类别的答案。

@interface UINavigationController (RotationAll)
-(NSUInteger)supportedInterfaceOrientations;
@end   


@implementation UINavigationController (RotationAll)
  -(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
  }
@end

回答by krason

Forgive the verbosity, but I think your answer is below.

请原谅冗长,但我认为您的答案如下。

I was having the same problem, but it was occurring on all IOS 6 devices including my iphone 4. Reading your question actually answered mine. I had removed this ages ago because it wasn't doing anything:

我遇到了同样的问题,但它发生在所有 IOS 6 设备上,包括我的 iphone 4。阅读您的问题实际上回答了我的问题。我很久以前就删除了它,因为它没有做任何事情:

- (NSUInteger)supportedInterfaceOrientations{    
    NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);
    return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
            UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown); }

and my code was working perfectly in prior ios releases. It appears this has become manditory in ios 6. But only for upsideDown. The other orientations were working fine.

我的代码在之前的 ios 版本中运行良好。看来这在 ios 6 中已成为强制性要求。但仅适用于颠倒。其他方向工作正常。

So, thank you.

所以谢谢。

Back to your problem. Have you reviewed your project target to see if all "Supported Interface Orientations" are dark gray? I'm wondering if upside down is light gray, (ie, unchecked). I found in a quick test that on prior ios releases these are ignored too. As long as you return YES from shouldAutorotateToInterfaceOrientation it will support the orientations, but not in iOS 6.

回到你的问题。您是否查看过您的项目目标以查看是否所有“支持的界面方向”都是深灰色的?我想知道倒置是否为浅灰色(即未选中)。我在一个快速测试中发现,在之前的 ios 版本中,这些也被忽略了。只要您从 shouldAutorotateToInterfaceOrientation 返回 YES,它就会支持方向,但在 iOS 6 中不支持。

Lastly, you have implemented shouldAutorotate, which I have not in my code. I think this defaults to Yes. And I think this just tells the os to try to rotate, but then you have to tell it which orientations you support with shouldAutorotateToInterfaceOrientation. But your snippet doesn't show that you've implemented shouldAutorotateToInterfaceOrientation. I would suggest implementing this as it tells the OS which orientations you support returning a simple YES means you support all.

最后,您实现了 shouldAutorotate,我的代码中没有。我认为这默认为 Yes。而且我认为这只是告诉操作系统尝试旋转,但是您必须告诉它您使用 shouldAutorotateToInterfaceOrientation 支持哪些方向。但是您的代码段并未显示您已经实现了 shouldAutorotateToInterfaceOrientation。我建议实现这一点,因为它告诉操作系统您支持哪些方向,返回一个简单的 YES 意味着您支持所有方向。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    //return (interfaceOrientation == UIInterfaceOrientationPortrait);
    return YES;
}

Hope this helps.

希望这可以帮助。

回答by avishic

Default target settings is no upside-down orientation, so try this:

默认目标设置没有颠倒方向,所以试试这个:

Go to Project file select your app target select "Summery" tab check that all orientation types pressed

转到项目文件选择您的应用程序目标选​​择“摘要”选项卡检查是否按下了所有方向类型

Good luck :)

祝你好运 :)

回答by Razvan

If you plan to enable or disable rotation for all view controllers you don't need to subclass UINavigationController. Instead use:

如果您计划为所有视图控制器启用或禁用旋转,则不需要继承 UINavigationController。而是使用:

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

in your AppDelegate.

在您的 AppDelegate 中。

If you plan to support all orientations in your app but different orientations on PARENT View Controllers (UINavigationController stack for example) you should use

如果您计划在您的应用程序中支持所有方向,但在父视图控制器(例如 UINavigationController 堆栈)上支持不同的方向,您应该使用

   -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window 

from AppDelegate in combination with the following methods in your PARENT View Controller.

从 AppDelegate 结合您的父视图控制器中的以下方法。

    - (BOOL)shouldAutorotate

and

- (NSUInteger)supportedInterfaceOrientations

But if you plan to have different orientation settings in different CHILDREN ViewControllers in the same navigation stack (like me) you need to check the current ViewController in the navigation stack.

但是,如果您计划在同一个导航堆栈中的不同 CHILDREN ViewControllers 中设置不同的方向设置(像我一样),您需要检查导航堆栈中的当前 ViewController。

I've created the following in my UINavigationController subclass:

我在 UINavigationController 子类中创建了以下内容:

    - (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    int interfaceOrientation = 0;

    if (self.viewControllers.count > 0)
    {
        id viewController;
        DLog(@"%@", self.viewControllers);
        for (viewController in self.viewControllers)
        {
            if ([viewController isKindOfClass:([InitialUseViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else if ([viewController isKindOfClass:([MainViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else
            {
                 interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }
    return interfaceOrientation;
}

Since you cannot control anymore from children ViewControllers the rotation settings of presented view controller you must somehow intercept what view controller is currently in the navigation stack. So that's what I did :). Hope that helps !

由于您无法再从子 ViewControllers 控制呈现的视图控制器的旋转设置,因此您必须以某种方式拦截当前在导航堆栈中的视图控制器。所以这就是我所做的:)。希望有帮助!