iPhone 横向常见问题解答和解决方案 [英] iPhone Landscape FAQ and Solutions

查看:23
本文介绍了iPhone 横向常见问题解答和解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有很多混乱和一组相应的问题,关于如何实现正确处理横向/纵向模式自动旋转的 iPhone 应用程序.当需要以横向模式启动时,实现这样的应用程序尤其困难.最常见的观察效果是布局混乱,屏幕区域无法再识别触摸.

There has been a lot of confusion and a set of corresponding set of questions here on SO how iPhone applications with proper handling for Landscape/Portrait mode autorotation can be implemented. It is especially difficult to implement such an application when starting in landscape mode is desired. The most common observed effect are scrambled layouts and areas of the screen where touches are no longer recognized.

对标记为 iphonelandscape 的问题的简单搜索揭示了这些问题,这些问题在某些情况下会发生:

A simple search for questions tagged iphone and landscape reveals these issues, which occur under certain scenarios:

  • Landscape only iPhone app with multiple nibs: App started in Landscape mode, view from first nib is rendered fine, everything view loaded from a different nib is not displayed correctly.

Iphone 横向模式在加载新控制器时切换到纵向模式:不言自明

Iphone Landscape mode switching to Portraite mode on loading new controller: Self explanatory

iPhone:仅限横向,之后首先 addSubview,UITableViewController 没有正确旋转:与上述相同的问题.

iPhone Landscape-Only Utility-Template Application:布局错误,控制器似乎无法识别应旋转的视图,但在横向模式下显示裁剪的纵向视图,导致一半屏幕保持空白.

iPhone Landscape-Only Utility-Template Application: Layout errors, controller does not seem to recognize the view should be rotated but displays a clipped portrait view in landscape mode, causing half of the screen to stay blank.

presentModalViewController 横向后纵向 viewController:模态视图是也没有正确呈现.

presentModalViewController in landscape after portrait viewController: Modal views are not correctly rendered either.

已经提出了一组不同的解决方案,其中一些包括通过 CoreGraphics 完全自定义的动画,而另一些则建立在观察从主笔尖加载的第一个视图控制器始终正确显示的基础上.

A set of different solutions have been presented, some of them including completely custom animation via CoreGraphics, while others build on the observation that the first view controller loaded from the main nib is always displayed correct.

我花了大量时间调查这个问题,最终找到了一个不仅是部分解决方案,而且应该在所有这些情况下都能工作的解决方案.我打算在这篇 CW 帖子中为其他在横向模式下遇到 UIViewControllers 问题的人提供一些常见问题解答.

I have spent a significant amount of time investigating this issue and finally found a solution that is not only a partial solution but should work under all these circumstances. It is my intend with this CW post to provide sort of a FAQ for others having issues with UIViewControllers in Landscape mode.

请提供反馈并通过纳入任何相关观察来帮助提高此帖子的质量.如果您知道任何答案,请随时编辑和发布其他/更好的答案.

Please provide feedback and help improve the quality of this Post by incorporating any related observations. Feel free to edit and post other/better answers if you know of any.

推荐答案

文档:

在您的视图控制器中,覆盖 shouldAutorotateToInterfaceOrientation: 以声明您支持的界面方向.每次设备方向更改时,控制器基础结构都将/应该检查此属性.

What's in the documentation:

In your view controller, override shouldAutorotateToInterfaceOrientation: to declare your supported interface orientations. This property will/should be checked by the controller infrastructure everytime the device orientation changes.

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)orientation
{
   return  (orientation == UIInterfaceOrientationLandscapeRight);
}

这是您的视图控制器需要做的绝对最小值.如果您想以横向模式启动您的应用程序,您需要将以下键添加到您的 .plist 文件中:

This is the absolute minimum your view controller needs to do. If you want to launch your application in landscape mode, you need to add the following key to your .plist file:

<key>UIInterfaceOrientation</key>
<string>UIInterfaceOrientationLandscapeRight</string>

Apple 建议在横向右侧模式下启动仅限横向的应用程序(请参阅 用户体验指南下的 HIG > 立即开始).

Apple recommends starting landscape only applications in Landscape Right mode (see the HIG under User Experience Guidelines > Start Instantly).

每次您尝试加载不同于从主笔尖加载的视图控制器时,您的视图控制器既不会询问其支持的界面方向,也不会正确设置其框架.只有绑定到窗口的第一个视图控制器才会正确布局.

Everytime you try to load a different view controller other than that loaded from the main nib, your view controller is neither interrogated about it's supported interface orientations nor is its frame set correctly. Only the first view controller bound to the window will be layed out correctly.

其他人建议使用MasterViewController"连接到主窗口,其他控制器将它们的视图作为子视图添加到主窗口,而不是直接连接到窗口中.虽然我发现这个解决方案是一个可行的选择,但在将模态视图控制器添加到这些子视图的情况下,它无法正常工作.如果您有一些子视图应该能够自动旋转(主控制器会阻止什么),也会出现问题.

Other people have suggested using a "MasterViewController" hooked up to the main window to which other controllers add their views as subviews instead of hooking directly into the window. While I have found this solutions is a viable option, it does not work correctly in the case of modal view controllers added to those said subviews. There's also a problem if you have some subviews that should be able to autorotate (what the master controller will prevent).

使用未记录的 API 来强制特定的界面方向也不是一种选择.

The usage of undocumented API's to force a certain interface orientation is not an option either.

迄今为止我发现的最佳解决方案是修改MasterViewController";解决方法.不是使用自定义的MasterViewController",而是使用带有隐藏导航栏和隐藏标签栏的 UINavigationController.如果所有其他视图都从此控制器的导航堆栈中推送/弹出,则该堆栈上的控制器的自动旋转将得到正确管理.

The best solution I have found so far is a modification of the "MasterViewController" workaround. Instead of using a custom "MasterViewController", a UINavigationController with hidden Navigation Bar and hidden Tab Bar is used. If all other views are pushed/popped from the navigation stack of this controller, auto-rotations of controllers on that stack will be managed correctly.

通过 presentModalViewController:animated: 来自 UINavigationController 导航堆栈上的任何视图控制器呈现的模态控制器将被旋转并以正确的布局呈现.如果你希望你的模态视图控制器可以旋转到与父视图控制器不同的方向,你需要从 parentshouldAutorotateToInterfaceOrientation 方法返回所需的方向控制器,同时呈现模态视图.为了在模态控制器关闭时正确恢复界面方向,您需要确保 shouldAutorotateToInterfaceOrientation 在调用 dismissModalViewController:animated: 之前返回父控制器所需的方向.您可以在视图控制器上使用私有 BOOL 来管理它(例如 BOOL isModalMailControllerActive_).

Modal controllers presented via presentModalViewController:animated: from any of the view controllers on the UINavigationController's navigation stack will be rotated and rendered with correct layout. If you want your modal view controller to be rotatable to a different orientation than that of the parent view controller, you need to return the desired orientation from the shouldAutorotateToInterfaceOrientation method of the parent controller while the modal view is presented. In order to properly restore the interface orientation when the modal controller is dismissed, you need to make sure shouldAutorotateToInterfaceOrientation returns the desired orientation for the parent controller before you call dismissModalViewController:animated:. You can use a private BOOL on your view controller to manage that (e.g. BOOL isModalMailControllerActive_).

我会尽快添加一段示例代码,现在为时已晚.如果还有任何未解决的问题或这篇文章有任何不清楚的地方,请告诉我.随时编辑和改进.

I'll add a piece of sample code soon, It's just to late now. Please let me know if any unresolved issues remain or anything is unclear about this post. Feel free to edit and improve.

这篇关于iPhone 横向常见问题解答和解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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