まず第一に、UIViewController が埋め込まれているコントローラーに大きく依存します。
たとえば、UINavigationController の内部にある場合、その UINavigationController をサブクラス化して、このような方向メソッドをオーバーライドする必要がある場合があります。
サブクラス化された UINavigationController (階層の最上位のビューコントローラーが方向を制御します) は、self.window.rootViewController として設定する必要があります。
- (BOOL)shouldAutorotate
{
return self.topViewController.shouldAutorotate;
}
- (NSUInteger)supportedInterfaceOrientations
{
return self.topViewController.supportedInterfaceOrientations;
}
iOS 6 から、UINavigationController がその UIVIewControllers に向きのサポートを要求しないようになりました。したがって、サブクラス化する必要があります。
さらに
次に、PORTRAITモードのみが必要なUIViewControllersの場合、これらの関数を記述します
- (BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskPortrait);
}
LANDSCAPE も必要とする UIViewController の場合は、マスキングを All に変更します。
- (NSUInteger)supportedInterfaceOrientations
{
return (UIInterfaceOrientationMaskAllButUpsideDown);
//OR return (UIInterfaceOrientationMaskAll);
}
ここで、向きが変わったときに何らかの変更を加えたい場合は、この関数を使用します。
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
}
非常に重要
AppDelegate で、これを記述します。これは非常に印象的です。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskAll);
}
すべてのビューコントローラーにポートレート モードのみを提供する場合は、ポートレート マスクを適用します。つまり、UIInterfaceOrientationMaskPortrait
それ以外の場合、一部の UIViewController を縦長のままにし、他の UIViewController をすべての向きに対応させたい場合は、ALL マスクを適用します。つまり、UIInterfaceOrientationMaskAll