在iOS6中的单个UIViewController上禁用自动旋转

Jes*_*ala 19 xcode objective-c uiinterfaceorientation autorotate ios6

我有一个项目使用UINavigationControllersegues工作正常,所有这些都正确旋转,事情是......我只是想禁用autorotation特定的UIViewController.我试过这个:

- (BOOL)shouldAutorotateToInterfaceOrientation:
                               (UIInterfaceOrientation)interfaceOrientation {    
    return NO;
}

// New Autorotation support for iOS 6.
- (BOOL)shouldAutorotate NS_AVAILABLE_IOS(6_0){
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
Run Code Online (Sandbox Code Playgroud)

但它不起作用,我UIViewController会自动旋转,欢迎任何帮助:)

Gay*_*DDS 35

根据View Controller编程指南

如果要暂时禁用自动旋转,请避免操作方向掩码来执行此操作.而是覆盖初始视图控制器上的shouldAutorotate方法.在执行任何自动旋转之前调用此方法.如果返回NO,则抑制旋转.

因此,您需要子类化'UINavigationController',实现shouldAutorotate并在故事板中使用导航控制器类.

- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[DetailViewController class]])
        return NO;

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 对于某些人来说这可能是显而易见的,但如果您没有使用UINavigationController来控制应用程序的导航层次结构,则此解决方案不起作用. (3认同)

Jes*_*ala 15

要完成GayleDDS的新手答案,只需添加一个UINavigationController的子类,就像他建议的那样:

#import "UINavigationController.h"
#import "MonthCalendarVC.h"

@implementation UINavigationController (overrides)
- (BOOL)shouldAutorotate
{
    id currentViewController = self.topViewController;

    if ([currentViewController isKindOfClass:[MonthCalendarVC class]])
        return NO;

    return YES;
}
@end
Run Code Online (Sandbox Code Playgroud)

MonthCalendarVC是我想要处于纵向模式(固定)的viewController,然后只是将导入添加到我的appdelegate.m

#import "UINavigationController.h"
Run Code Online (Sandbox Code Playgroud)

就是这样


Chr*_*phe 5

看看这个其他方法:

http://www.sebastianborggrewe.de/only-make-one-single-view-controller-rotate/

您只需在ViewController中实现canRotate以允许旋转.

适用于iOS 7.

2015-01-30 因为塞巴斯蒂安的网站似乎不起作用(404错误),这是我对其解决方案的解释:

与sebastian不同,我更喜欢使用协议(比如C#中的接口)来避免在我的每个视图控制器中创建一个方法" - (void)canrotate:".

IRotationCapabilities.h
-----------------------

#ifndef NICE_APPS_IRotationCapabilities_h
#define NICE_APPS_IRotationCapabilities_h

@protocol IRotationCapabilities < NSObject >

// Empty protocol

@end

#endif


FirstViewController.h
---------------------

- ( void )viewWillAppear:( BOOL )animated
{
    [ super viewWillAppear:animated ];

    // Forces the portrait orientation, if needed
    if( ![ self conformsToProtocol:@protocol( IRotationCapabilities ) ] )
    {
        if( self.navigationController.interfaceOrientation != UIInterfaceOrientationPortrait )
        {
            [ [ UIDevice currentDevice ] setValue:@( 1 ) forKey:@"orientation" ];
        }
    }
}

SecondViewController.h
-----------------------

#import "IRotationCapabilities.h"

@interface SecondViewController : UIViewController < IRotationCapabilities >


AppDelegate.m
-------------

#pragma mark - Orientation management

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

    if( __iPhone )
    {
        // Gets topmost/visible view controller
        UIViewController * currentViewController = [ self topViewController ];

        // Checks whether it implements rotation
        if( [ currentViewController conformsToProtocol:@protocol( IRotationCapabilities ) ] )
        {
            // Unlock landscape view orientations for this view controller
            return ( UIInterfaceOrientationMaskAllButUpsideDown );
        }

        // Allows only portrait orientation (standard behavior)
        return ( UIInterfaceOrientationMaskPortrait );
    }
    else
    {
        // Unlock landscape view orientations for iPad
        return ( UIInterfaceOrientationMaskAll );
    }
}
Run Code Online (Sandbox Code Playgroud)