每个视图控制器后推新控制器。这意味着不可能让某些视图控制器仅支持肖像,而其他视图控制器仅支持肖像。换句话说,相同的导航控制器堆栈上的所有视图控制器应返回相同的委托:
(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