如何正确设计多方位iPad应用程序 [英] How to properly design multi-orientation iPad application

查看:126
本文介绍了如何正确设计多方位iPad应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在设计多方位iPad应用程序的正确方法是什么?我已经阅读了很多Apple文档,网络资源和一些SO Q& A.以下是我的初始要求:

What's the correct way of designing multi-orientation iPad app nowadays? I've read a lot of Apple docs, web resources and some SO Q&A. Here are my initial requirements:


  • 这必须适用于iOS 5及更高版本。无需创建与previos版本iOS的兼容性。

  • 我希望尽可能在不同的NIB文件中定义纵向和横向UI。

  • 我的NIB文件将针对不同方向的相同UI元素具有不同的图像(就像我将 header.png header-landscape.png UIImageView例如。

  • 应用程序将有几个屏幕,我需要能够在每个屏幕上切换方向。

  • This has to work on iOS 5 and above. No need to create back compatibility with previos versions of iOS.
  • I would like if possible to have portrait and landscape UI defined in different NIB files.
  • My NIB files will have different images for same UI elements in different orientation (like I will have header.png and header-landscape.png UIImageView for example.
  • App will have several screens and I need to be able to switch orientation on each of them.

那我该怎么办?


  • 在每个屏幕上创建一个VC并替换 willRotate 处理程序中的基础视图?

  • 每个方向创建一个VC?但是如何切换它们正确吗?

  • 简单地重新安排元素是行不通的(我认为),因为我必须重新加载图像。

  • 在代码中写入所有内容(我真的很讨厌这个想法)?

  • Create one VC per screen and replace underlying view in willRotate handler?
  • Create one VC per orientation? But then how do you switch them properly?
  • Simply re-arranging elements won't work (I think) because I would have to reload images.
  • Write everything in code (I would really hate this idea)?

截至今天这个问题的正确方法是什么?

What are proper approaches to this issue as of today?

推荐答案

我认为这是一个非常复杂的主题,就像大多数建筑问题一样。我不相信你应该尝试只用一种技术解决问题。

I actually think this is a really complex subject, like most architectural issues. I don't believe you should try to solve the problem with only one technique.

为了不排除本页上的所有其他答案,我在我的博客上发布了一篇完整的文章也是一些示例代码的链接

For the sake of not crowding off all the other answers on this page, I posted a full-writeup on my blog and a link to some sample code, too

应该首选在.xib文件中表达您的UI,尽管您允许自己偏离此范围的程度部分取决于将来修改您的应用程序的人员的技能组合。它可能不只是程序员

It should be preferred to express your UI in .xib files, although the extent to which you allow yourself to diverge from this partly depends on the skill set of the people who will modify your app in the future. It may not just be programmers!

它应该是强烈的首选使用一个.xib文件和一个 UIViewController 子类实现一个逻辑视图。努力做到这一点真的很难。在XIB的根视图上设置 autoresizesSubviews = YES ,并正确调整子视图' autoresizingMask 随着屏幕尺寸/方向的变化灵活变通可能会有很长的路要走。

It should be strongly preferred to implement one logical view with one .xib file, and one UIViewController subclass. Try really hard to do that. Setting autoresizesSubviews=YES on your XIB's root view, and properly adjusting the child views' autoresizingMask to flex with changes in screen size/orientation can go a long way.

但是,这并不总是足够的。 如果您的布局需要横向调整,超出自动调整可以处理的范围,我使用两个主要选项。你应该选择哪一个取决于你观点的内容。

But, that won't always be enough. If your layout needs adjusting in landscape orientation, beyond what autoresizing can handle, I use two main options. Which one you should choose depends on your view's content.

如果肖像与布局相对横向并没有太大的不同,那么我建议使用一个.xib,并在View Controller中使用一些代码来调整旋转时的布局。

If the layout for portrait vs. landscape is not too different, then I recommend staying with one .xib, and having a little bit of code in your View Controller to adjust layout on rotation.

如果横向与纵向差异非常重要,那么我建议每个方向都有一个.xib(具有严格的命名约定,如 MyViewController .xib MyViewController-landscape.xib )。但是,两个.xib文件都应该将文件所有者连接到相同的View Controller 类!对我而言,这是关键。

If the landscape vs. portrait differences are really significant, then I recommend having one .xib for each orientation (with a strict naming convention, like MyViewController.xib and MyViewController-landscape.xib). But, both .xib files should connect File's Owner to the same View Controller class! To me, this is key.

如果您要做除了首选替代方案之外的任何事情,我建议创建一个可重复使用的 UIViewController 自动执行此操作的基类,并保持一致。这比你最初想象的要多得多,而且在每个需要轮换处理的 UIViewController 子类中继续这样做是愚蠢的。

If you are ever going to do anything but the preferred alternative, I recommend creating a reusable UIViewController base class to automate this, and keep it consistent. It's more work than you might think at first, and it's silly to keep doing it in every UIViewController subclass that needs rotation handling.

我创建了这样一个基类,并且将它放在一个示例项目中。您可以看到一个Hello World示例,说明我应该如何处理所有三个场景:

I created such a base class, and put it in a sample project here. You can see a Hello World example of how I think all three scenarios should be handled:


  • 一个视图控制器和一个.xib,仅自动调整(我的 FirstViewController

  • 一个视图控制器和两个.xibs,它们之间自动切换(我的 SecondViewController

  • 一个视图控制器和一个.xib,在旋转时具有次要的程序布局( ThirdViewController

  • One view controller and one .xib, with autoresizing only (my FirstViewController)
  • One view controller and two .xibs, with switching between them automated (my SecondViewController)
  • One view controller and one .xib, with minor programmatic layout on rotations (ThirdViewController)

RotatingV我使用的iewController 基类同样适用于iPhone应用程序。我实际上有一个更复杂的版本来处理维护iPad iPhone,肖像横向布局(适用于通用应用)。

The RotatingViewController base class I use is equally applicable to iPhone apps. I actually have a more complicated version that handles maintaining iPad and iPhone, portrait and landscape layouts (for Universal apps).

但是,这个问题只是关于iPad,所以我把它剥离了,以便更容易理解。

But, this question was only about iPad, so I stripped it down, to be easier to understand.

我的基类还有一个实用程序 imageNamed:有助于加载适合当前方向的图像的方法(以 image-landscape.png 约定命名)。但是,我认为应该使用可伸缩的UIImages 而不是大部分时间。

My base class also has a utility imageNamed: method that helps load images that are proper for the current orientation (named with a image-landscape.png convention). However, I think stretchable UIImages should be used instead the vast majority of the time.

我没有这样做,但 RotatingViewController 也可以尝试走子视图树并更新 UIButton UIImageView上的图像属性对象,当设备方向改变时。我没有那么长,但你可以。

I didn't do this, but the RotatingViewController could also try to walk its subviews tree and update the image property on UIButton or UIImageView objects, when device orientation changes. I didn't go to that length, but you could.

这些推荐的更多基本原理 在我参考的博客文章中

这篇关于如何正确设计多方位iPad应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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