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

查看:200
本文介绍了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.

简单搜索标记为 iphone的问题 landscape 显示在某些情况下会出现的这些问题:

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横向模式在加载新控制器时切换到Portraite模式:
自解释

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

iPhone:仅在横向上,在第一次addSubview之后,UITableViewController无法正常旋转:与abov相同的问题e。

iPhone: In landscape-only, after first addSubview, UITableViewController doesn’t rotate properly: Same issue as above.

iPhone仅限横向实用程序 - 模板应用程序:布局错误,控制器似乎无法识别视图应该旋转但在横向模式下显示剪切的纵向视图,导致一半的屏幕保持空白。

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.

纵向viewController之后的 presentModalViewController :模式视图也未正确呈现。

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建议在Landscape Right模式下启动仅横向应用程序(参见 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.

最佳解决方案I到目前为止已发现修改了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.

通过<$显示模态控制器来自 UINavigationController 的任何视图控制器的c $ c> presentModalViewController:animated:将以正确的布局旋转和渲染。如果您希望模态视图控制器可以旋转到与父视图控制器不同的方向,则需要从 shouldAutorotateToInterfaceOrientation 方法返回所需的方向显示模态视图时,>父控制器。为了在模态控制器关闭时正确恢复界面方向,您需要确保 shouldAutorotateToInterfaceOrientation 在调用 dismissModalViewController:动画:。您可以在视图控制器上使用私有 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天全站免登陆