19

我有一个项目使用UINavigationController并且segues工作正常,它们都正确旋转,问题是......我只想禁用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;
}

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

4

5 回答 5

35

根据视图控制器编程指南

如果您想暂时禁用自动旋转,请避免操作方向蒙版来执行此操作。相反,覆盖初始视图控制器上的 shouldAutorotate 方法。在执行任何自转之前调用此方法。如果它返回 NO,则旋转被抑制。

所以你需要继承'UINavigationController',实现shouldAutorotate并在你的故事板中使用你的导航控制器类。

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

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

    return YES;
}
于 2013-06-29T06:04:02.173 回答
14

将完成 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

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

#import "UINavigationController.h"

就是这样

于 2013-07-01T15:18:16.217 回答
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 );
    }
}
于 2014-02-11T15:31:52.143 回答
4

尝试在你的 UIViewController 中实现它:

// implements the interface orientation (iOS 6.x)
@interface UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations;
@end

@implementation UINavigationController (RotationNone)
-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}
@end
于 2013-06-29T21:05:15.360 回答
0

我看到有人在 Swift 中问过这个问题。这不是很明显,因为 Objective-C 方法根本不是 Swift 中的方法,而是计算变量:

override var shouldAutorotate: Bool { return false }
override var supportedInterfaceOrientations: UIInterfaceOrientationMask { return .portrait }
于 2019-07-20T10:19:02.310 回答