什么是“正确”的在iOS 8中处理方向更改的方法? [英] What is the "right" way to handle orientation changes in iOS 8?

查看:218
本文介绍了什么是“正确”的在iOS 8中处理方向更改的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我在iOS 8中使用纵向和横向界面方向的正确或最佳方法吗?似乎我想要用于此目的的所有功能都在iOS 8中被弃用,而我的研究似乎没有明确,优雅的替代方案。如果我们处于纵向或横向模式,我真的应该查看宽度和高度以确定自己吗?

Can someone please tell me the "right" or "best" approach to working with portrait and landscape interface orientations in iOS 8? It seems that all the functions I want to use for that purpose are deprecated in iOS 8, and my research has turned up no clear, elegant alternative. Am I really supposed to look at the width and height to determine for myself if we are in portrait or landscape mode?

例如,在我的视图控制器中,我该怎么办?实现以下伪代码?

For example, in my view controller, how should I implement the following pseudocode?

if we are rotating from portrait to landscape then
  do portrait things
else if we are rotating from landscape to portrait then
  do landscape things


推荐答案

Apple建议使用大小类作为可用屏幕空间大小的粗略度量,以便UI可以显着改变其布局和外观。考虑到纵向的iPad具有与横向相同的尺寸等级(常规宽度,常规高度)。这意味着您的UI在两个方向之间应该或多或少相似。

Apple recommends using size classes as a coarse measure of how much screen space is available, so that your UI can significantly change its layout and appearance. Consider that an iPad in portrait has the same size classes as it does in landscape (Regular width, Regular height). This means that your UI should be more or less similar between the two orientations.

然而,iPad中从纵向到横向的变化非常重要,您可能需要对UI进行一些较小的调整,即使大小类没有改变。由于不推荐使用 UIViewController 上与接口方向相关的方法,Apple现在建议在 UIViewController 中实现以下新方法:替换:

However, the change from portrait to landscape in an iPad is significant enough that you may need to make some smaller adjustments to the UI, even though the size classes have not changed. Since the interface orientation related methods on UIViewController have been deprecated, Apple now recommends implementing the following new method in UIViewController as a replacement:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id <UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    // Code here will execute before the rotation begins.
    // Equivalent to placing it in the deprecated method -[willRotateToInterfaceOrientation:duration:]

    [coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Place code here to perform animations during the rotation.
        // You can pass nil or leave this block empty if not necessary.

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        // Code here will execute after the rotation has finished.
        // Equivalent to placing it in the deprecated method -[didRotateFromInterfaceOrientation:]

    }];
}

太棒了!现在你在旋转开始之前以及完成之后得到回调。但实际上知道旋转是纵向还是横向呢?

Great! Now you're getting callbacks right before the rotation starts, and after it finishes. But what about actually knowing whether the rotation is to portrait or to landscape?

Apple建议将旋转视为父视图大小的变化。换句话说,在iPad从纵向旋转到横向旋转期间,您可以将其视为根级视图,只需从更改其 bounds.size {768,1024} {1024,768} 。知道了这一点,你应该使用 size 传递到上面的 viewWillTransitionToSize:withTransitionCoordinator:方法来判断你是否是旋转到纵向或横向。

Apple recommends thinking about rotation as simply a change in size of the parent view. In other words, during an iPad rotation from portrait to landscape, you can think of it as the root-level view simply changing its bounds.size from {768, 1024} to {1024, 768}. Knowing this then, you should use the size passed into the viewWillTransitionToSize:withTransitionCoordinator: method above to figure out whether you are rotating to portrait or landscape.

如果您想要一种更加无缝的方式将旧代码迁移到新的iOS 8工作方式,请考虑使用这个简单类别,可用于根据视图大小确定视图是纵向还是横向。

If you want an even more seamless way to migrate legacy code to the new iOS 8 way of doing things, consider using this simple category on UIView, which can be used to determine whether a view is "portrait" or "landscape" based on its size.

回顾:


  1. 您应该使用大小类来确定何时显示根本不同的UI (例如,类似iPhone的用户界面与类似iPad的用户界面)

  2. 如果您需要在尺寸类不要<时对UI进行较小的调整/ em>更改,但您的容器(父视图)大小,例如当iPad旋转时,在UIV中使用 viewWillTransitionToSize:withTransitionCoordinator:回调iewController。

  3. 应用程序中的每个视图都应该根据布局给出的空间做出布局决策。让视图的自然层次结构将此信息级联下来。

  4. 同样,不要使用 statusBarOrientation - 这基本上是设备级属性 - 来确定是否为肖像vs风景。状态栏方向只应由处理 UIWindow 之类的代码使用,这些代码实际上位于应用程序的根本级别。

  1. You should use size classes to determine when to show fundamentally different UIs (e.g. an "iPhone-like" UI vs. an "iPad-like" UI)
  2. If you need to make smaller adjustments to your UI when size classes don't change but your container (parent view) size does, such as when an iPad rotates, use the viewWillTransitionToSize:withTransitionCoordinator: callback in UIViewController.
  3. Every view in your app should only make layout decisions based on the space that it has been given to layout in. Let the natural hierarchy of views cascade this information down.
  4. Similarly, don't use the statusBarOrientation -- which is basically a device-level property -- to determine whether to layout a view for "portrait" vs "landscape". The status bar orientation should only be used by code dealing with things like UIWindow which actually live at the very root level of the app.

这篇关于什么是“正确”的在iOS 8中处理方向更改的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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