web-dev-qa-db-ja.com

UITabBarを使用してiOS 6で単一のUIViewControllerを自動回転させる

私はPortrait Modeでのみ機能するアプリを持っていますが、ビデオを表示できるsingleViewがあるので、landscape modeでもそのビューを機能させたいのですが、iOS 6ではどのように動作するかわかりません私はそれを行うことができます、今私はこれを持っています:

AppDelegate.mで私は:

self.window.rootViewController = myTabBar;

次に、プロジェクトの概要で:

enter image description here

そして私はiOS 6でビューの回転を検出するためにこれをしなければならないことを発見しました:

- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}

// Tell the system It should autorotate
- (BOOL) shouldAutorotate {
return YES;
}

だから私は私のUIViewControllerにのみ上記のコードを挿入します。横向きでも使用したいのですが、機能しません。誰でも私がそれを行う方法を知っていますか?ビデオを表示するときに自動回転させたいだけです。

25
Piero

まず、ターゲット設定は次のようになります。 Supported Interface Orientations

UITabBarControllerで:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}

-(NSUInteger)supportedInterfaceOrientations
{
    if (self.selectedViewController) 
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate
{
    return YES;
}

ViewController内:

a)回転したくない場合:

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

- (BOOL)shouldAutorotate
{
    return NO;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

b)横向きに回転したい場合:

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

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAllButUpsideDown;
}

編集:

他の解決策は、AppDelegate内にこのメソッドを実装することです。

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

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

私はコメントを書くつもりですが、できないので、これを回答として投稿します。

これは私のシナリオでした:

私のアプリは特定のビューでのみ向きの変更をサポートしており、私が望むもののためだけにそれを行う方法を理解することができませんでした、そして私はこの質問に着手し、mientusの答えを見ました(これに感謝します)、そして私は先に進んで何をしました彼はサブクラスUITabBarControllerであり、これらのメソッドをオーバーライドすることを提案しました:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

    NSLog(@"AUTO ROTATE IN CUSTOM TAB BAR");
    // You do not need this method if you are not supporting earlier iOS Versions
    return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}


-(NSUInteger)supportedInterfaceOrientations{

    NSLog(@"supportedInterfaceOrientations IN CUSTOM TAB BAR");

    if (self.selectedViewController)
        return [self.selectedViewController supportedInterfaceOrientations];

    return UIInterfaceOrientationMaskPortrait;
}

-(BOOL)shouldAutorotate{

    NSLog(@"shouldAutorotate IN CUSTOM TAB BAR");
    return [self.selectedViewController shouldAutorotate];
}

次に、各ビューコントローラー内に、回転が必要かどうかを示すメソッドがあります。 UITabBarControllerのメソッドが呼び出されていましたが、viewcontrollerのメソッドは呼び出されていませんでした。そのため、必要のない場所でも回転が行われていました。次に、UINavigationControllerをサブクラス化し、同じメソッドをオーバーライドして、supportedInterfaceOrientationをこのように変更するだけで、次のようになります。

- (NSUInteger)supportedInterfaceOrientations{

NSLog(@"supportedInterfaceOrientations IN CUSTOM NAV BAR CALLING CURRENT VIEW CONTROLLER");
UIViewController* presented = [[self viewControllers] lastObject];
return [presented supportedInterfaceOrientations];

}

これが基本的に行うことは、現在のビューコントローラーを取得し、サポートされている方向を要求し、ビューコントローラーのメソッドが呼び出されると、必要な場所で方向を処理できることです。

3
Oscar Salvador

縦向きのみのビューのサブビューを回転するビューですか?通常、ビューの回転動作はrootviewcontrollerから継承されます。次に、rootviewcontrollerのshouldAutorotateでNOを返すと、すべてのアンダービューで回転を停止します。

アーキテクチャをこのように分割することをお勧めします:

rootViewController-> supportedInterfaceOrientations = Portrait&shouldAutorotate = YES NORotationViewControllers-> supportedInterfaceOrientations = Portrait&shouldAutorotate = YES rotationViewControllers-> supportedInterfaceOrientations = All&shouldAutorotate = YES

これをまだ読んでいない場合は、以下をご覧ください。 Supporting Multiple Interface Orientations

0