web-dev-qa-db-ja.com

iOS 6ローテーション:supportedInterfaceOrientationsが機能しませんか?

IOS 6 SDKでこの問題が発生しています:回転を許可する必要があるビュー(ビデオビューなど)があります。これで、アプリのInfo.plistですべての向きを確認し、各ViewControllerで整理する必要があることがわかりました。しかし、うまくいきません!アプリは常に、Info.plistで指定された方向に回転します。

Info.plist:

<key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>

回転を許可しないViewController:

//deprecated
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}

観察:アプリは横向きと縦向きに回転します。私が間違っているのはなぜですか?

乾杯、マーク

編集:私の最新の調査結果は、アプリのある時点で回転させたい場合、haveを使用して、プロジェクト設定またはInfo.plistで4つの回転方向すべてをアクティブにすることも示しています。これに代わる方法は、オーバーライドすることです

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

info.plistをオーバーライドするAppDelegateで。 Info.plistでPortraitのみを設定し、shouldAutorotateToInterfaceOrientationまたはsupportedInterfaceOrientationsをオーバーライドすることにより、一部のViewControllerで回転させることはできなくなりました。

36
stk

ViewControllerがUINavigationControllerまたはUITabBarControllerの子である場合、問題の原因は親です。あなたはあなたの質問で示したようにそれらのInterfaceOrientationメソッドをオーバーライドするだけで、その親View Controllerをサブクラス化する必要があるかもしれません

編集:

ポートレートのみのTabBarControllerの例

           @interface MyTabBarController : UITabBarController
            {
            }
            @end

            @implementation MyTabBarController

            // put your shouldAutorotateToInterfaceOrientation and other overrides here        
            - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
                return (interfaceOrientation == UIInterfaceOrientationPortrait);
            }

            - (NSUInteger)supportedInterfaceOrientations{ 
                return UIInterfaceOrientationMaskPortrait; 
            } 

        @end
31
CSmith

上記のCSmithの答えに加えて、UINavigationControllerサブクラスの次のコードにより、最初にこれが機能すると予想される方法で、トップビューコントローラーに委任できます。

- (BOOL)shouldAutorotate;
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    if ([[self topViewController] respondsToSelector:@selector(supportedInterfaceOrientations)])
        return [[self topViewController] supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}
27
Evan Schoenberg

CSmithのアプローチに代わる別の方法を次に示します。

ナビゲーションスタック/タブバーのすべてのビューが許容される向きのセットに同意する必要があるiOS 6より前の動作を複製する場合は、これをUITabBarControllerまたはUINavigationControllerのサブクラスに入れます。

- (NSUInteger)supportedInterfaceOrientations
{
    NSUInteger orientations = [super supportedInterfaceOrientations];

    for (UIViewController *controller in self.viewControllers)
        orientations = orientations & [controller supportedInterfaceOrientations];

    return orientations;
}
8
Glenn Schmidt

このカテゴリを追加してみてください:

@interface UINavigationController(InterfaceOrientation)

@end

@implementation UINavigationController(InterfaceOrientation)

- (NSUInteger) supportedInterfaceOrientations {
    if (self.viewControllers.count > 0)
        return [[self.viewControllers objectAtIndex:0] supportedInterfaceOrientations];
    else
        return UIInterfaceOrientationMaskAll;
}

@end
2
Shimanski Artem

UINavigationControllerとSwiftを使用しているユーザーの場合、この拡張機能をプロジェクトに追加できます。その後、Navigation Controllerは、子コントローラーにコントロールを委任します。

extension UINavigationController {
    override public func supportedInterfaceOrientations()
    -> UIInterfaceOrientationMask {
        if let ctrl = topViewController {
            return ctrl.supportedInterfaceOrientations()
        }
        return super.supportedInterfaceOrientations()
    }

    override public func shouldAutorotate() -> Bool {
        if let ctrl = topViewController {
            return ctrl.shouldAutorotate()
        }
        return super.shouldAutorotate()
    }
}
1
Alvivi

@ -Alviviの回答がSwift 4に更新されました。

extension UINavigationController {

    // Look for the supportedInterfaceOrientations of the topViewController
    // Otherwise, viewController will rotate irrespective of the value returned by the ViewController
    override open var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if let ctrl = self.topViewController {
            return ctrl.supportedInterfaceOrientations
        }
        return super.supportedInterfaceOrientations
    }

    // Look for the shouldAutorotate of the topViewController
    // Otherwise, viewController will rotate irrespective of the value returned by the ViewController
    override open var shouldAutorotate: Bool {
        if let ctrl = self.topViewController {
            return ctrl.shouldAutorotate
        }
        return super.shouldAutorotate
    }
}
0
Harikrishna Pai

@CSmithおよび@EvanSchoenbergへの追加。

回転するビューと回転しないビューがある場合は、UITabBarControllerのカスタムインスタンスを作成し、それぞれのUIViewControllerで決定する必要があります。

- (BOOL)shouldAutorotate;
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    UIViewController * top;
    UIViewController * tab = self.selectedViewController;
    if([tab isKindOfClass:
        ([UINavigationController class])]) {
        top = [((UINavigationController *)tab)
                 topViewController];
    }

    if ([top respondsToSelector:@selector(supportedInterfaceOrientations)])
        return [top supportedInterfaceOrientations];
    else
        return [super supportedInterfaceOrientations];
}
0
SwiftArchitect