presentViewController在iOS 6中不支持方向

时间:2021-08-18 11:44:43

I am using this code

我正在使用此代码

if ([self respondsToSelector:@selector(presentViewController:animated:completion:)])
    {
        [self presentViewController:navigationControllerCustom animated:YES completion:nil];
    }
    else
    {
        [self presentModalViewController:navigationControllerCustom animated:YES];
    }

My application has two orientation Portrait and Portrait upside down. This code works well with iOS 5.1, but orientation does not work on iOS 6

我的应用程序有两个方向纵向和纵向颠倒。此代码适用于iOS 5.1,但方向在iOS 6上不起作用

I have also added this code on my navigationControllerCustom class

我还在navigationControllerCustom类中添加了这段代码

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

Please help me to solve this issue.

请帮我解决这个问题。

Thanks in advance.

提前致谢。

5 个解决方案

#1


25  

You must include this in you application delegate:

您必须将此包含在您的应用程序委托中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

Also make sure the View Controller's both have the following, works fine for me.

还要确保View Controller的两者都具有以下功能,对我来说工作正常。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

The documentation also says that UINavigationController's doesn't query its top View Controller for orientations supported, although an Apple engineer over on the Developer Forums did say so... it seems that it does not. Therefore you should add a category for UINavigationController, this is the one I use:

该文档还说UINavigationController没有查询其*View Controller的支持方向,尽管开发人员论坛上的Apple工程师确实这么说......似乎它没有。因此,您应该为UINavigationController添加一个类别,这是我使用的类别:

#import "UINavigationController+autorotate.h"

@implementation UINavigationController (autorotate)

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

@end

For more information how AutoRotate works on iOS 6 check out this answer

有关AutoRotate如何在iOS 6上运行的更多信息,请查看此答案

#2


2  

You forgot:

你忘了:

- (BOOL)shouldAutorotate {
    return YES;
}

#3


1  

Just copy paste that snippet above your code

只需将代码段上方的代码段复制粘贴即可

@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}

@end

#4


1  

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

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

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

in combination with the following methods in your Parent View Controller.

与父视图控制器中的以下方法结合使用。

    - (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控制所呈现的视图控制器的旋转设置,您必须以某种方式拦截视图控制器当前在导航堆栈中的内容。这就是我做的:)。希望有所帮助!

#5


1  

Subclass UINavigationController and override the shouldAutorotate and supportedInterfaceOrientations like this:

子类UINavigationController并覆盖如下的shouldAutorotate和supportedInterfaceOrientations:

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

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

And now you can control your orientation is each ViewController. Just override the two methods in each ViewController.

现在你可以控制每个ViewController的方向。只需覆盖每个ViewController中的两个方法。

#1


25  

You must include this in you application delegate:

您必须将此包含在您的应用程序委托中:

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
    return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}

Also make sure the View Controller's both have the following, works fine for me.

还要确保View Controller的两者都具有以下功能,对我来说工作正常。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

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

The documentation also says that UINavigationController's doesn't query its top View Controller for orientations supported, although an Apple engineer over on the Developer Forums did say so... it seems that it does not. Therefore you should add a category for UINavigationController, this is the one I use:

该文档还说UINavigationController没有查询其*View Controller的支持方向,尽管开发人员论坛上的Apple工程师确实这么说......似乎它没有。因此,您应该为UINavigationController添加一个类别,这是我使用的类别:

#import "UINavigationController+autorotate.h"

@implementation UINavigationController (autorotate)

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

@end

For more information how AutoRotate works on iOS 6 check out this answer

有关AutoRotate如何在iOS 6上运行的更多信息,请查看此答案

#2


2  

You forgot:

你忘了:

- (BOOL)shouldAutorotate {
    return YES;
}

#3


1  

Just copy paste that snippet above your code

只需将代码段上方的代码段复制粘贴即可

@implementation UINavigationController (rotation)
//temp hack for iOS6, this allows passing supportedInterfaceOrientations to child viewcontrollers.
- (NSUInteger)supportedInterfaceOrientations {
return [self.topViewController supportedInterfaceOrientations];
}

@end

#4


1  

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

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

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

in combination with the following methods in your Parent View Controller.

与父视图控制器中的以下方法结合使用。

    - (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控制所呈现的视图控制器的旋转设置,您必须以某种方式拦截视图控制器当前在导航堆栈中的内容。这就是我做的:)。希望有所帮助!

#5


1  

Subclass UINavigationController and override the shouldAutorotate and supportedInterfaceOrientations like this:

子类UINavigationController并覆盖如下的shouldAutorotate和supportedInterfaceOrientations:

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

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

And now you can control your orientation is each ViewController. Just override the two methods in each ViewController.

现在你可以控制每个ViewController的方向。只需覆盖每个ViewController中的两个方法。