17

回転の問題がある iOS 6 に更新している iPhone アプリがあります。私はUITabBarControllerwith 16を持っていUINavigationCotrollersます。ほとんどのサブビューは縦向きまたは横向きで機能しますが、一部は縦向きのみです。iOS 6 では、回転してはいけないときに回転しています。

supportedInterfaceOrienations現在のナビゲーションコントローラーの選択されたビューコントローラーを返すために、タブバーコントローラーをサブクラス化しようとしました:

- (NSUInteger)supportedInterfaceOrientations{

    UINavigationController *navController = (UINavigationController *)self.selectedViewController;
    return [navController.visibleViewController supportedInterfaceOrientations];
}

これで近づきました。View Controller が表示されているときに位置がずれることはありませんが、横向きでタブを切り替えると、新しいタブがサポートされていなくても横向きになります。

理想的には、アプリは、現在表示されているビュー コントローラーのサポートされている向きにのみ配置されます。何か案は?

4

4 に答える 4

58

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
{
    return [self.selectedViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

UINavigationControllerこれらのメソッドをオーバーライドしてサブクラス化します。

-(NSUInteger)supportedInterfaceOrientations
{
    return [self.topViewController supportedInterfaceOrientations];
}

-(BOOL)shouldAutorotate
{
    return YES;
}

次に、回転させたくないビューコントローラーにこれらのメソッドを実装します。

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

-(BOOL)shouldAutorotate
{
    return NO;
}

-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

そして、回転させたいviewControllerの場合:

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

    -(NSUInteger)supportedInterfaceOrientations
    {
        return UIInterfaceOrientationMaskAllButUpsideDown;
    }

    -(BOOL)shouldAutorotate
    {
        return YES;
    }

tabbarController は、アプリ ウィンドウの RootviewController として追加する必要があります。デフォルトの向きをサポートする予定の場合は、逆さま以外はすべて iPhone のデフォルトであるため、他に何もする必要はありません。逆さまをサポートしたい場合、または別の向きをサポートしたくない場合は、アプリのデリゲートや info.plist で適切な値を設定する必要があります。

于 2012-09-20T02:48:00.840 に答える
5

ナビゲーション スタックの一部のビュー コントローラーはすべての向きをサポートし、一部は縦向きのみをサポートするという問題がありましたが、UINavigationControllerアプリでサポートされているすべての向きを返していました。この小さなハックが役に立ちました。これが意図した動作なのか、それとも何なのかわかりません

@implementation UINavigationController (iOS6OrientationFix)

-(NSUInteger) supportedInterfaceOrientations {
    return [self.topViewController supportedInterfaceOrientations];
}

@end
于 2012-09-20T11:45:23.170 に答える
3

そのようなものが良いと思います(カテゴリメソッドとして)

-(NSUInteger) supportedInterfaceOrientations {
    if([self.topViewController respondsToSelector:@selector(supportedInterfaceOrientations)])
    {
        return [self.topViewController supportedInterfaceOrientations];
    }
    return UIInterfaceOrientationMaskPortrait;
}

これにより、メソッドが確実に実装されます。このチェックを行わず、メソッドが実装されていない場合 (iOS5 env のように)、アプリはクラッシュするはずです!

于 2012-10-02T12:15:30.827 に答える
0

すべてのビュー コントローラーの回転を有効または無効にする場合は、サブクラス化する必要はありませんUINavigationController。代わりに次を使用します。

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

あなたのAppDelegate

アプリですべての向きをサポートするが、PARENT ビュー コントローラー (UINavigationControllerスタックなど) で異なる向きをサポートする予定がある場合は、使用する必要があります。

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

AppDelegatePARENT View Controller の次のメソッドと組み合わせて使用​​します。

    - (BOOL)shouldAutorotate

- (NSUInteger)supportedInterfaceOrientations

ただし、同じナビゲーション スタック (私のように) 内の異なる CHILDREN ViewController で異なる方向設定を行う予定がある場合は、ナビゲーション スタック内の現在の ViewController を確認する必要があります。

UINavigationControllerサブクラスで次のものを作成しました。

    - (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    int interfaceOrientation = 0;

    if (self.viewControllers.count > 0)
    {
        DLog(@"%@", self.viewControllers);
        for (id viewController in self.viewControllers)
        {
            if ([viewController isKindOfClass:([InitialUseViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else if ([viewController isKindOfClass:([MainViewController class])])
            {
                 interfaceOrientation = UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
            }
            else
            {
                 interfaceOrientation = UIInterfaceOrientationMaskAllButUpsideDown;
            }
        }
    }
    return interfaceOrientation;
}

子ViewControllersから提示されたView Controllerの回転設定を制御することはできなくなるため、現在ナビゲーションスタックにあるView Controllerを何らかの方法でインターセプトする必要があります。それが私がしたことです:)。それが役立つことを願っています!

于 2013-01-20T00:51:03.753 に答える