19

私はプロジェクトを使用してUINavigationControllerおり、適切に動作しています。それらはすべて正しく回転します。問題は...特定のseguesで無効にしたいだけです。私はこれを試しました:autorotationUIViewController

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

View Controller プログラミング ガイドによると

自動回転を一時的に無効にする場合は、方向マスクを操作してこれを行うことは避けてください。代わりに、最初のビュー コントローラーで 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.sebastianborgrewe.de/only-make-one-single-view-controller-rotate/

回転を許可するには、ViewController に canRotate を実装するだけです。

iOS 7 で問題なく動作します。

2015-01-30 sebastian のサイトが動作していないようです (404 エラー) ため、これはその解決策の私の解釈です:

セバスチャンとは異なり、私はプロトコル (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 に答える