简体   繁体   English

如何使用一个故事板用于带有自动布局(ios6 + ios7)的4英寸和3.5英寸iphone屏幕?

[英]how can I use one storyboard for 4“ and 3.5” iphone screens with autolayout (ios6 + ios7)?

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. 虽然有很多关于构建故事板布局的问题和答案,它可以在4“和3.5”屏幕尺寸上工作,但我找不到适合以下场景的解决方案。

I use autolayout with ios6 (and ios7), and I don't have to support landscpae or ipad. 我使用autolayout和ios6(和ios7),我不需要支持landscpae或ipad。 I have a UIView with several subviews, which have to look as the mockup below. 我有一个带有几个子视图的UIView,它们必须看作下面的模型。 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? 我的问题是 - 如何根据运行时的屏幕大小使autolayout选择正确的约束?

屏幕MOCKUPS

Note, that I DONT want to use 2 different storyboards. 请注意,我不想使用2个不同的故事板。 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. 屏幕上的变化需要我做两倍的工作。

I have tried 2 methods to make this work on one storyboard, but I'm not satisfied with either of them. 我已经尝试了两种方法在一个故事板上完成这项工作,但我对其中任何一个都不满意。

  • Double the constraints. 加倍约束。 The larger constraint (50) has a higher priority than the lower constraint (30). 较大的约束(50)具有比较低约束(30)更高的优先级。 The problem with this approach is that on the 3.5" screen size, autolayout may pick a just few lower priority constraints - enough to satisfy the layout - but leave some high priority constraints. 这种方法的问题在于,在3.5英寸屏幕尺寸上,自动布局可能只选择几个较低优先级的约束 - 足以满足布局 - 但保留一些高优先级约束。

双重约束

  • Subclass NSLayoutConstraint. 子类NSLayoutConstraint。 In this method, all the constraints in the storyboard are set to be NSDualLayoutConstraint. 在此方法中,故事板中的所有约束都设置为NSDualLayoutConstraint。 In in initialization code of NSDualLayoutConstraint, the constant of the constraint is changed to the value of 3_5_constant in case the runtime screen size is 3.5". this method is more deterministic, but you can't see the 3.5" layout in the interface builder preview. 在NSDualLayoutConstraint的初始化代码中,如果运行时屏幕大小为3.5“,则约束的常量将更改为3_5_constant的值。此方法更具确定性,但您无法在界面构建器预览中看到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? 如果只有界面构建器约束具有将在屏幕大小为3.5“时应用的辅助常量值,那么它将解决我的问题。所以我仍然问我的问题 - 如何正确使用单个故事板来正确布局其子视图屏幕尺寸为4“和3.5”?

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. 如果您只想使用一个故事板而又不想使用自动布局,这会使您的图表中显示的内容变得更加容易,您将不得不在代码中布局视图。

You will just need to detect if the user is running on a 4" device and layout your views accordingly. 您只需要检测用户是否在4“设备上运行并相应地布局视图。

#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
}

However, if you were to use autolayout for this, you'd find it's going to save you a ton of time and code. 但是,如果你为此使用autolayout,你会发现它会为你节省大量的时间和代码。 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. 使用上面提到的解决方案而不必手动布局代码中的每个视图,您只需要根据设备更新y和高度约束。

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. 考虑到这个图表显示了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];

This stackoverflow answer has solved the same problem for me. 这个stackoverflow的答案为我解决了同样的问题。

Key is make an IBOutlet to the constraint (eg height). 关键是使IBOutlet达到约束(例如高度)。 Then do something like this 然后做这样的事情

- (void)updateViewConstraints {
    [super updateViewConstraints];

    self.myConstraintFromIB.constant =
        [UIScreen mainScreen].bounds.size.height > 480.0f ? 200 : 100;
}

You will need to edit your constraints programatically. 您需要以编程方式编辑约束。 You may find this question helpful for that. 您可能会发现此问题对此有帮助。

You could also have two separate Storyboard files, or two viewController scenes in one storyboard file. 您还可以在一个storyboard文件中有两个单独的Storyboard文件或两个viewController场景。

How about adding an extra view and including the square view inside it? 如何添加额外的视图并在其中包含方形视图? The extra view should be transparent so no-one sees it. 额外的视图应该是透明的,所以没有人看到它。 Set the constraints of the extra view to expand with the screen size and set the constraints of the square view to be centred in the extra box. 设置额外视图的约束以使用屏幕大小进行扩展,并将方形视图的约束设置为在额外框中居中。 Select a multiplier for the square view size relative to the extra box 选择相对于额外框的方形视图大小的乘数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM