每個視圖控制器後推新控制器。這意味着不可能讓某些視圖控制器僅支持肖像,而其他視圖控制器僅支持肖像。換句話說,相同的導航控制器堆棧上的所有視圖控制器應返回相同的委託:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
但有一個簡單的解決這個!這是一個從縱向到橫向的例子。下面是執行它的步驟,下面是代碼來支持它。
- 創建一個'假'視圖控制器,它將作爲子導航控制器中的根。這個視圖控制器應該支持風景。
- 創建
UINavigationController
的新實例,添加「假」的視圖控制器作爲根的實例和橫向視圖控制器作爲第二視圖控制器
- 目前
UINavigationController
實例爲從父視圖控制器 模式的一個實例
首先,創建一個新的視圖控制器(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));
}
下面是代碼來呈現你想要顯示在視圖控制器風景模式:
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));
}
你能分享樣本代碼嗎? – 2012-12-12 06:20:39