我怎么可以用一个故事板为4英寸和3.5英寸iphone屏幕,自动布局(iOS6的+ ios7)? [英] how can I use one storyboard for 4" and 3.5" iphone screens with autolayout (ios6 + ios7)?

查看:150
本文介绍了我怎么可以用一个故事板为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。在该方法中,在故事板中的所有约束都设定为NSDualLayoutConstraint。在NSDualLayoutConstraint初始化code,约束的不断更改为3_5_constant值的情况下运行时的屏幕尺寸为3.5,这种方法比较确定的,但你不能看到3.5布局界面生成器preVIEW。

如果只Interface Builder的限制不得不将应用于辅助恒定值,当屏幕尺寸为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?

推荐答案

如果您希望只使用一个故事板,你不希望使用自动布局,这将使你的生活轻松了许多对你已经证明什么您的图表,你将不得不布局你的意见,code。

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
}

不过,如果你使用自动布局对于这一点,你会发现它会为您节省大量的时间和code。而不必用我上面提到的解决方案,以手动布局code每一个观点,你只需要根据更新设备上的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天全站免登陆