ios - 在 UINavigationController 中仅支持一个 View 的横向

标签 ios iphone cocoa-touch uinavigationcontroller uiinterfaceorientation

我有一个导航 Controller ,它有几个 View Controller 。我需要支持所有 View Controller 的所有方向,除了一个只支持横向的特殊 View Controller 。这个特殊的 View Controller 出现在导航堆栈的中间。我做了很多研究,但找不到任何好的解决方案。以下是我已阅读和尝试过的链接。

http://www.iphonedevsdk.com/forum/iphone-sdk-development/3219-force-landscape-mode-one-view.html#post60435

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

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

  1. 创建一个“假” View Controller ,它将作为子导航 Controller 的根。此 View Controller 应支持横向。
  2. 创建 UINavigationController 的新实例,将“假” View Controller 的实例添加为根,将横向 View Controller 的实例添加为第二个 View Controller
  3. 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/

相关文章:

ios - 在 NodeJs HTTPS 中使用 certificates.cer

iphone - 动态填充 UITableView

iphone - 如何在 iOS 终止应用程序之前在过期处理程序中优雅地结束任务?

iphone - 获取字符串的语言

ios - NSUserDefaults initWithSuiteName 在删除应用程序后仍然存在

iphone - UIWebView 和定义字典

ios - 如何使用 referenceSizeForHeaderInSection 调整 collectionView header 的大小?

ios - 使用 SwiftUI 将单个图钉添加到 Mapkit

iphone - 如何在 ios 中调整 uilabel

objective-c - 洗牌和取消洗牌 NSMutableArray(找到正确的索引)