在UINavigationController中仅支持一个视图的格局

Pok*_*com 13 iphone cocoa-touch uinavigationcontroller uiinterfaceorientation ios

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

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以显示特殊视图控制器.然后我将在特殊视图控制器内创建一个新的导航视图控制器来推送后续的视图控制器.

如果有人有更好的想法,请告诉我.非常感谢!

更新:我已成功地使用我上述方法:更换pushViewControllerpresentModalViewController,并创建一个新的导航控制器.

Mar*_*sen 9

推入导航控制器堆栈的每个视图控制器必须支持相同的方向.这意味着不可能让一些视图控制器仅支持肖像,而其他视图控制器仅支持横向.换句话说,同一导航控制器堆栈上的所有视图控制器应在委托中返回相同的内容:

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Run Code Online (Sandbox Code Playgroud)

但是有一个简单的解决方案!这是一个从肖像到风景的例子.以下是执行此操作的步骤,以下是支持它的代码.

  1. 创建一个"伪"视图控制器,它将作为子导航控制器的根.此视图控制器应支持格局.
  2. 创建a的新实例,UINavigationController以root身份添加"假"视图控制器的实例,并将横向视图控制器的实例添加为第二个视图控制器
  3. 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));
} 
Run Code Online (Sandbox Code Playgroud)

以下是显示您希望以横向模式显示的视图控制器的代码:

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];
Run Code Online (Sandbox Code Playgroud)

请记住,landscapeViewController也应具有此实现:

(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   return (interfaceOrientation == UIInterfaceOrientationIsLandscape(interfaceOrientation));
} 
Run Code Online (Sandbox Code Playgroud)