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