我如何将一个故事板用于 4 英寸和 3.5 英寸iphone 屏幕自动布局(ios6 + ios7)? [英] how can I use one storyboard for 4" and 3.5" iphone screens with autolayout (ios6 + ios7)?

查看:21
本文介绍了我如何将一个故事板用于 4 英寸和 3.5 英寸iphone 屏幕自动布局(ios6 + ios7)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然关于构建适用于 4" 和 3.5" 屏幕尺寸的故事板布局有很多问题和答案,但我找不到适合以下场景的解决方案.

While there are many questions and answers about building a storyboard layout that would work both on 4" and 3.5" screen sizes, I couldn't find a suitable solution for the following scenario.

我在 ios6(和 ios7)中使用自动布局,我不必支持 landscpae 或 ipad.我有一个带有几个子视图的 UIView,它们必须看起来像下面的模型.在故事板中设置自动布局约束以适应任何一种屏幕尺寸都很容易.我的问题是 - 如何让自动布局在运行时根据屏幕尺寸选择正确的约束?

I use autolayout with ios6 (and ios7), and I don't have to support landscpae or ipad. I have a UIView with several subviews, which have to look as the mockup below. It is easy to set up autolayout constraints in the storyboard to fit either of the screen sizes. My question is - how do I make autolayout choose the correct constraints depending on the screen size at runtime?

请注意,我不想使用 2 个不同的故事板.在我的整个应用程序中这样做需要做很多工作,而且我必须连接每个故事板上的所有代表、出口和动作.换一个屏幕需要我做双倍的工作.

Note, that I DONT want to use 2 different storyboards. Doing so across my whole application would be a lot of work, and I would have to hook up all the delegates, outlets and actions on each storyboard. A change in a screen would require me to do double the work.

我尝试了 2 种方法来在一个情节提要上完成这项工作,但我对其中任何一种都不满意.

I have tried 2 methods to make this work on one storyboard, but I'm not satisfied with either of them.

  • 双重约束.较大的约束 (50) 具有比较低的约束 (30) 更高的优先级.这种方法的问题在于,在 3.5 英寸的屏幕尺寸上,自动布局可能会选择一些较低优先级的约束——足以满足布局——但会留下一些高优先级的约束.

  • 子类 NSLayoutConstraint.在这个方法中,storyboard 中的所有约束都设置为 NSDualLayoutConstraint.在 NSDualLayoutConstraint 的初始化代码中,将约束的常量更改为 3_5_constant 的值,以防运行时屏幕大小为 3.5".这种方法更具确定性,但在界面生成器预览中看不到 3.5" 布局.

如果只有界面构建器约束有一个辅助常量值,当屏幕尺寸为 3.5" 时将应用它,它会解决我的问题..所以我的问题仍然存在 - 如何正确使用单个故事板为 4" 和 3.5" 屏幕尺寸正确布局其子视图?

If only interface builder constraints had a secondary constant value that would be applied when the screen size is 3.5", it would solve my problem.. So I remain with my question - how can I properly use a single storyboard to layout its subviews correctly for 4" AND 3.5" screen sizes?

推荐答案

如果您只想使用一个故事板并且不想使用自动布局,这将使您的生活更轻松你的图表,你将不得不在代码中布局你的视图.

If you want to use only one storyboard and you do not want to utilize auto layout, which would make your life a lot easier for what you've shown in your diagram, you will have to layout your views in code.

您只需要检测用户是否在 4" 设备上运行并相应地布局您的视图.

You will just need to detect if the user is running on a 4" device and layout your views accordingly.

#define IS_568_SCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)

if (IS_568_SCREEN) {
    // Lots of code to layout for 4" devices
} else {
    // Lots of code to layout for 3.5" devices
}

但是,如果您为此使用自动布局,您会发现它会为您节省大量时间和代码.不必使用我上面提到的解决方案在代码中手动布局每个视图,您只需根据设备更新 y 和高度约束.

However, if you were to use autolayout for this, you'd find it's going to save you a ton of time and code. Instead of having to manually layout every view in code using the solution I mentioned above, you'd simply need to update the y and height constraints depending on the device.

考虑到这个图表显示了自动布局将为您处理什么以及您需要手动更新什么,这应该有助于更好地了解使用自动布局可以节省多少.

Considering this diagram showing what autolayout would handle for you and what you'd need to update manually, this should help paint a better picture of just how much you'll save with utilizing autolayout.

#define IS_568_SCREEN (fabs((double)[[UIScreen mainScreen]bounds].size.height - (double)568) < DBL_EPSILON)

if (IS_568_SCREEN) {
    self.layoutConstraintY.constant = 50.0f;
    self.layoutConstraintHeight.constant = 248.0f;
} else {
    self.layoutConstraintY.constant = 30.0f;
    self.layoutConstraintHeight.constant = 220.0f;
}
[self layoutIfNeeded];

这篇关于我如何将一个故事板用于 4 英寸和 3.5 英寸iphone 屏幕自动布局(ios6 + ios7)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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