ナビゲーション コントローラー スタックにプッシュされるすべてのビュー コントローラーは、同じ向きをサポートする必要があります。これは、一部のビュー コントローラーが縦向きのみをサポートし、他のビュー コントローラーが横向きのみをサポートすることはできないことを意味します。つまり、同じナビゲーション コントローラー スタック上のすべてのビュー コントローラーは、デリゲートで同じものを返す必要があります。
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
しかし、これには簡単な解決策があります!これは、縦向きから横向きにする例です。これを行う手順は次のとおりです。以下はそれをサポートするコードです。
- サブ ナビゲーション コントローラーのルートになる「偽の」ビュー コントローラーを作成します。このView Controllerは横向きをサポートする必要があります。
- の新しいインスタンスを作成し
UINavigationController
、「偽の」ビュー コントローラーのインスタンスをルートとして追加し、ランドスケープ ビュー コントローラーのインスタンスを 2 番目のビュー コントローラーとして追加します。
UINavigationController
親View Controllerからインスタンスをモーダルとして提示する
まず、次のコードで新しいビュー コントローラー (FakeRootViewController) を作成します。
@interface FakeRootViewController : UIViewController
@property (strong, nonatomic) UINavigationController* parentNavigationController;
@end
@implementation FaceRootViewController
@synthesize parentNavigationController;
// viewWillAppear is called when we touch the back button on the navigation bar
(void)viewWillAppear:(BOOL)animated {
// Remove our self from modal view though the parent view controller
[parentNavigationController dismissModalViewControllerAnimated:YES];
}
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
横向きモードで表示したいView Controllerを表示するコードは次のとおりです。
FakeRootViewController* fakeRootViewController = [[FakeRootViewController alloc] init];[fakeRootViewController.navigationItem setBackBarButtonItem:backButton]; // Set back button
// The parent navigation controller is the one containing the view controllers in portrait mode.
fakeRootViewController.parentNavigationController = parentNavigationController;
UINavigationController* subNavigationController = // Initialize this the same way you have initialized your parent navigation controller.
UIViewController* landscapeViewController = // Initialize the landscape view controller
[subNavigationController setViewControllers:
[NSArray arrayWithObjects:fakeRootViewController,
landscapeViewController, nil] animated:NO];
[_navigationController presentModalViewController:subNavigationController animated:YES];
landscapeViewController にもこの実装が必要であることに注意してください。
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
}