presentViewController not supporting orientation i

2019-02-01 05:48发布

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

I have also added this code on my navigationControllerCustom class

- (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条回答
神经病院院长
2楼-- · 2019-02-01 06:17

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
查看更多
beautiful°
3楼-- · 2019-02-01 06:31

Subclass UINavigationController and override the shouldAutorotate and supportedInterfaceOrientations like this:

-(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.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-01 06:32

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

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

in your 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

   -(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.

I've created the following in my UINavigationController subclass:

    - (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 !

查看更多
beautiful°
5楼-- · 2019-02-01 06:33

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.

- (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:

#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

查看更多
祖国的老花朵
6楼-- · 2019-02-01 06:39

You forgot:

- (BOOL)shouldAutorotate {
    return YES;
}
查看更多
登录 后发表回答