我有一个导航控制器,它有几个视图控制器.我需要支持所有视图控制器的所有方向,除了一个只支持横向的特殊视图控制器.这个特殊的视图控制器出现在导航堆栈的中间.我做了很多研究,但找不到任何好的解决方案.以下是我阅读并尝试过的链接.
I have a navigation controller which have a few view controllers. I need to support all orientations for all view controllers except one special view controller which only supports landscape. This special view controller appears in the middle of the navigation stack. I have done quite a lot of research but couldn't find any good solution. Here are the links that I have read and tried.
http://www.iphonedevsdk.com/forum/iphone-sdk-development/3219-force-landscape-mode-one-view.html#post60435
如何将屏幕旋转为横向?一个>
如何从纵向模式自动旋转到横向模式?iPhone - 只允许在一个视图控制器上横向显示http://goodliffe.blogspot.com/2009/12/iphone-forcing-uiview-to-reorientate.html
接下来我将尝试用 presentModalViewController
替换导航控制器,以显示特殊的视图控制器.然后我将在特殊的视图控制器内部创建一个新的导航视图控制器来推送后续的视图控制器.
Next I am going to try to replace navigation controller with presentModalViewController
in order to display the special view controller. Then I am going to create a new navigation view controller inside the special view controller to push the subsequent view controllers.
如果有人有更好的想法,请告诉我.非常感谢!
If anyone has a better idea, please let me know. Really appreciated!
更新:我已经成功使用了我上面描述的方法:将 pushViewController
替换为 presentModalViewController
并创建一个新的导航控制器.
UPDATE: I have successfully use the method I described above: replace pushViewController
with presentModalViewController
and create a new navigation controller.
推送到导航控制器堆栈的每个视图控制器都必须支持相同的方向.这意味着不可能有一些视图控制器只支持纵向,而另一些只支持横向.换句话说,同一个导航控制器堆栈上的所有视图控制器都应该在委托中返回相同的结果:
Every view controller pushed onto the navigation controllers stack have to support the same orientations. This means that it is not possible to have some view controllers only supporting portrait and others only supporting landscape. In other words all view controllers on the same navigation controller stack should return the same in the delegate:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
但是有一个简单的解决方案!这是一个从纵向到横向的示例.这是执行此操作的步骤,下面是支持它的代码.
But there is a simple solution to this! Here is an example for going from portrait to landscape. Here is the steps to do it and below is code to support it.
UINavigationController
的新实例,将假"视图控制器的实例添加为 root,并将横向视图控制器的实例添加为第二个视图控制器UINavigationController
实例显示为模态UINavigationController
, add an instance of the ‘fake’ view controller as root and an instance of your landscape view controller as second view controllerUINavigationController
instance as modal from the parent view controller首先,使用以下代码创建一个新的视图控制器(FakeRootViewController):
First, create a new view controller (FakeRootViewController) with this code:
@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));
}
这里是呈现您希望在横向模式下显示的视图控制器的代码:
Here is the code to present the view controller that you wish to show in landscape mode:
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 也应该有这个实现:
Remember that the landscapeViewController should also have this implementation:
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
}
这篇关于UINavigationController 中仅支持一个视图的横向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!