1

フラグがあり、ユーザーは設定から​​設定します。No に設定すると、縦向きのみが許可されます。フラグが YES の場合、縦向きと横向きの両方が許可されます。iOS 5以下

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
    //[image_signature setImage:[self resizeImage:image_signature.image]];
    if(flag == YES)
        return (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight || toInterfaceOrientation == UIInterfaceOrientationPortrait);
    else
        return (toInterfaceOrientation == UIInterfaceOrientationPortrait);
}

しかし、iOS 6 以降では、この方法は推奨されていません。やってみた

-(BOOL)shouldAutorotate 
- (NSUInteger)supportedInterfaceOrientations 

しかし、成功しません。私を助けてください。

編集私はこれを試しました。

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    NSLog(@"preferredInterfaceOrientationForPresentation");
    return UIInterfaceOrientationMaskPortrait;
}

- (NSUInteger)supportedInterfaceOrientations
{
    NSLog(@"supportedInterfaceOrientations");
    return UIInterfaceOrientationMaskPortrait;
}

ただし、supportedInterfaceOrientations のみが 1 回だけ呼び出されます。シミュレーターの向きを変更すると、両方のメソッドが呼び出されません。

4

1 に答える 1

4
shouldAutorotateToInterfaceOrientation:
Returns a Boolean value indicating whether the view controller supports the specified orientation. 
(Deprecated in iOS 6.0. Override the supportedInterfaceOrientations and preferredInterfaceOrientationForPresentation methods instead.)

( http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/DeprecationAppendix/AppendixADeprecatedAPI.html )

したがって、新しいバージョンで使用できる 2 つの方法は次のとおりです。

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation

http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/preferredInterfaceOrientationForPresentation

- (NSUInteger)supportedInterfaceOrientations

この方法では、次のビットマスクを使用します。

UIInterfaceOrientationMaskPortrait

UIInterfaceOrientationMaskLandscapeLeft

UIInterfaceOrientationMaskLandscapeRight

UIInterfaceOrientationMaskPortraitUpsideDown

UIInterfaceOrientationMaskLandscape

UIInterfaceOrientationMaskAll

UIInterfaceOrientationMaskAllButUpsideDown

http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/supportedInterfaceOrientations

アップデート:

OK、なぜうまくいかないのかわかりません。

あなたがやろうとしていることを再現するデモアプリを作成しました:

(右クリック > 画像の URL をコピーし、新しいブラウザー タブで開いて大きな画像を表示します)

プロジェクトのセットアップ

方向フラグが NO の場合、ボタンのテキストに「横向きが拒否されました」と表示され、シミュレーターでデバイスを回転させても、期待どおりにインターフェイスが回転しません。

ランドスケープが拒否されました

ただし、ボタンをクリックして横向きを許可した後、シミュレータ デバイスを回転させると、予想どおり、実際にはインターフェイスの向きが変わります。

ここに画像の説明を入力

別のルート ビュー コントローラーに、現在のビュー コントローラーの方向デリゲート メソッドをオーバーライドする他の方向デリゲート メソッドはありませんか?

また、Auto Layout を使用すると干渉するかどうかもわかりませんが、使用しない傾向にあります。XIB ファイル内のビューで、"View" オブジェクトを選択し、インスペクターで" " のチェックを外しましUse Auto Layoutた。上記のサンプル プロジェクトのスクリーンショットは、自動レイアウトを使用していません。

更新 2:

さて、ウィンドウのルートビューコントローラーとしてナビゲーションコントローラーを使用するこのソリューションを見つけました(これを AppDelegate.m に入れます):

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
    NSUInteger orientations =UIInterfaceOrientationMaskAllButUpsideDown;

    if(self.window.rootViewController)
    {
        UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
        orientations = [presentedViewController supportedInterfaceOrientations];
    }
    return orientations;
}

この Stackoverflow の投稿で:

UiNavigationController の iOS 6 AutoRotate

ナビゲーション コントローラーを含む新しいスクリーンショット:

ここに画像の説明を入力

ここに画像の説明を入力

于 2013-08-19T12:10:26.970 に答える