IOS6 でオリエンテーションを強制する方法について多くのスレッドがあることは知っていますが、どれもうまくいかないようなので、これを理解するために助けが必要です。
多くのビュー コントローラーを持つナビゲーション ベースのアプリがあります。それらはすべて、横向きモードでロードする必要があるものを除いて縦向きビューです (ユーザーが最初に電話を回す必要はありません)。
ナビゲーション コントローラーの実装では、shouldAutorotate、supportedInterfaceOrientations、preferredInterfaceOrientationForPresentation を追加しました。
@implementation UINavigationController (Rotation_IOS6)
- (BOOL)shouldAutorotate
{
return [self.topViewController shouldAutorotate];
}
- (NSUInteger)supportedInterfaceOrientations
{
return [self.topViewController supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return [self.topViewController preferredInterfaceOrientationForPresentation];
}
@end
したがって、各View Controllerで定義された値を返します。
アプリ デリゲートには、supportedInterfaceOrientationsForWindow があります。
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
//Default orientations value
NSUInteger orientations = UIInterfaceOrientationMaskAllButUpsideDown;
//Get orientation from the last view controller
if(self.window.rootViewController){
UIViewController *presentedViewController = [[(UINavigationController *)self.window.rootViewController viewControllers] lastObject];
orientations = [presentedViewController supportedInterfaceOrientations];
}
return orientations;
}
各View Controllerには、そのビューの設定があります。次に例を示します。
// Only allow portrait view
-(NSInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskPortrait;
}
- (BOOL)shouldAutorotate {
return YES;
}
そして、次のビュー コントローラーを次のようにプッシュします。
NextViewController *nxtController = [[NextViewController alloc] initWithNibName:@"NextViewController" bundle:nil];
[self.navigationController pushViewController:nxtController animated:YES];
しかし、携帯電話を縦向きに持ったまま横向きビューコントローラーを押すと、縦向きモードでも読み込まれます。次に電話を傾けると、自動回転機能がトリガーされ、ビューが横向きモードに回転し、そのモードでロックされます。ただし、電話の向きを使用せずに横向きモードでロックして、自動回転を確認する必要があります。何か案は?