9

私のアプリのすべてのビューコントローラは、ポートレートまたはランドスケープのものを除いて、ポートレートの向きでのみ動作します。景観へiOS:ポップビューコントローラのインターフェイスの向きを変更する

  1. を私はUITabBarController
  2. に両方の向きで動作するコントローラを押す
  3. 撮りからユーザーの変化の向き
  4. ユーザーを押して、「戻るボタン」

私は、次のようないくつかの使用シナリオを持っています

これらの操作の後、アプリケーションは横向きのままで、自動的に縦向きに変更されません。

私は、supportedInterfaceOrientations(私はiOS 6.0を使用しています)を使用して、ビューコントローラの向きを制御します。私は間違っている?ユーザーが戻るボタンを押したときにアプリケーションが自動的に向きを変更できるようにするには、どうすれば正しい動作を得ることができますか?答えてくれてありがとう!

+0

このリンクをご覧ください[http://stackoverflow.com/questions/12650137/how-to-change-the-device-orientation-programmatically-in-ios-6](http://stackoverflow.com/questions/12650137/how-to-change-the-device-orientation-programmatically-in-ios-6) –

+0

このシナリオについては、Appleのドキュメント[here](https://developer.apple.com/library/)を参照してください。 /特派員/ ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html#// apple_ref/doc/uid/TP40007457-CH7-SW13)。 –

答えて

2

iOS 6(およびそれ以前のバージョン)では、デバイスが回転しているときにビューコントローラがオフスクリーンになっても、通知は行われません。トップビューコントローラーになると、willAnimateRotationToInterfaceOrientation:duration:も送られません。

ビューコントローラの現在の向きを追跡し、デバイスの向きをviewWillAppear:で確認する必要があります。それらが異なる場合は、willAnimateRotationToInterfaceOrientation:duration:を使用して正しく設定できます。

これは多くのことを行う可能性が高いため、View Controllerが継承する汎用スーパークラスを作成することができます。

典型的な解決策がある:

@implementation MyHandlesOffscreenRotationController 
{ 
    BOOL isShowingPortrait; 
} 

- (void) viewDidLoad 
{ 
    [super viewDidLoad]; 

    isShowingPortrait = UIInterfaceOrientationIsPortrait(
         [[UIApplication sharedApplication] statusBarOrientation]); 
} 


- (void) viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 

     BOOL currIsPortrait = UIInterfaceOrientationIsPortrait(
           [[UIApplication sharedApplication] statusBarOrientation]); 

    if ((isShowingPortrait && !currIsPortrait) || 
     (!isShowingPortrait && currIsPortrait)) { 
     [self willAnimateRotationToInterfaceOrientation: 
       [[UIApplication sharedApplication] statusBarOrientation] 
               duration:0.0f]; 
    } 
} 

@end 
+0

これはまともな答えです。あなたが回転した後に 'isShowingPortrait'を設定しなければならないので、更新されました。それ以外の場合は1回だけ動作します。 (ios7でもうまくいきます) – zero3nna

0

P.L.このトピックではすばらしい解決策があります:ポートレートのみを許可する空のモーダルビューコントローラを表示して即座に終了します。ただ、UINavigationControllerカテゴリ内-(BOOL)shouldAutoRotate and - (NSUInteger)supportedInterfaceOrientationsを上書き風景

How to change the device orientation programmatically in iOS 6

1

、その後のViewControllerは、他のViewControllerからポップした後、そのサポートの方向に回転させる強制されます。

@implementation UINavigationController (Rotate) 

- (BOOL)shouldAutorotate 
{ 
    return [self.topViewController shouldAutorotate]; 
} 

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

@end 
2

のiOS 9と

上記ポップの時には、ちょうどあなたのviewWillAppear方法で下記のコードを記述します。

[[UIDevice currentDevice] setValue:[NSNumber numberWithInteger: UIInterfaceOrientationPortrait]forKey:@"orientation"]; 

これで、あなたのビューはポートレートモードで表示されます。

関連する問題