我有一个导航 Controller ,它有几个 View Controller 。我需要支持所有 View Controller 的所有方向,除了一个只支持横向的特殊 View Controller 。这个特殊的 View Controller 出现在导航堆栈的中间。我做了很多研究,但找不到任何好的解决方案。以下是我已阅读和尝试过的链接。
How to rotate screen to landscape?
How to autorotate from portrait to landscape mode? iPhone - allow landscape orientation on just one viewcontroller http://goodliffe.blogspot.com/2009/12/iphone-forcing-uiview-to-reorientate.html
接下来我将尝试用 presentModalViewController
替换导航 Controller 以显示特殊 View Controller 。然后我将在特殊 View Controller 中创建一个新的导航 View Controller 来推送后续的 View Controller 。
如果有人有更好的想法,请告诉我。非常感谢!
更新:我已经成功地使用了我上面描述的方法:将 pushViewController
替换为 presentModalViewController
并创建一个新的导航 Controller 。
最佳答案
每个推送到导航 Controller 堆栈上的 View Controller 都必须支持相同的方向。这意味着不可能让一些 View Controller 只支持纵向,而另一些只支持横向。换句话说,同一个导航 Controller 堆栈上的所有 View Controller 都应该在委托(delegate)中返回相同的值:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
但是有一个简单的解决方案!这是一个从肖像到风景的例子。以下是执行此操作的步骤,下面是支持它的代码。
- 创建一个“假” View Controller ,它将作为子导航 Controller 的根。此 View Controller 应支持横向。
- 创建
UINavigationController
的新实例,将“假” View Controller 的实例添加为根,将横向 View Controller 的实例添加为第二个 View Controller - 将
UINavigationController
实例作为父 View Controller 的模态呈现
首先,使用以下代码创建一个新的 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));
}
关于ios - 在 UINavigationController 中仅支持一个 View 的横向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5023487/