在UINavigationController中仅支持一个视图的格局 [英] Support landscape for only one view in UINavigationController

查看:148
本文介绍了在UINavigationController中仅支持一个视图的格局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个导航控制器,它有几个视图控制器。除了一个只支持横向的特殊视图控制器外,我需要支持所有视图控制器的所有方向。此特殊视图控制器显示在导航堆栈的中间。我做了很多研究,但找不到任何好的解决方案。以下是我阅读并尝试过的链接。

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 - 仅在一个viewcontroller上允许横向显示
http://goodliffe.blogspot.co m / 2009/12 / iphone -forcing-uiview-to-reorientate.html

接下来我将尝试用<$ c替换导航控制器$ c> 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!

UPDATE:我已成功使用上述方法:替换 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.


  1. 创建一个'假'视图控制器,它将成为一个子导航控制器。此视图控制器应支持格局。

  2. 创建 UINavigationController 的新实例,添加'假'视图控制器的实例为root和一个横向视图控制器的实例作为第二个视图控制器

  3. UINavigationController 实例作为模式从父视图控制器出现

  1. Create a ‘fake’ view controller that will be root in a sub navigation controller. This view controller should support landscape.
  2. Create a new instance of a UINavigationController, add an instance of the ‘fake’ view controller as root and an instance of your landscape view controller as second view controller
  3. Present the UINavigationController 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中仅支持一个视图的格局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆