如何为不同的屏幕尺寸设置不同的自动布局约束

How to set up different Auto Layout constraints for different screen sizes(如何为不同的屏幕尺寸设置不同的自动布局约束)

本文介绍了如何为不同的屏幕尺寸设置不同的自动布局约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有静态单元格的 tableView.该单元格包含一个完全填充它的 ImageView.我在上面还有另一个更小的 ImageView.我用约束定位这个 ImageViews.我有一个关于调整约束大小的问题.如何在不编写 if/else 循环的情况下为不同的设备尺寸设置不同的约束常数.有没有办法在故事板中设置它?我对父布局有一个主要约束,例如常量值为 10.这对于 iPhone 5 屏幕来说没问题,但在 iPhone 6/6 plus 屏幕上它应该高于 10.

iPhone5 自动布局

iPhone6 自动布局

解决方案

不用写一行代码!

有一次我的初级开发人员问我同样的问题,当时我如何区分 iPhoneSE 和 iPhone6 的某些限制,只有一个解决方案在编写类似的东西

如果设备 == iPhoneSE {常数 = 44} else if device == iPhone6 {常数 = 52}

为了克服这个问题,我创建了一个库

第一步

将 NSLayoutHelper 分配给您的约束

第 2 步

为你想要的设备更新约束

第三步

运行应用程序并查看 MAGIC

I have a tableView with static cells. The cell contains an ImageView which fills it completely. And I have another smaller ImageViews atop. I position this ImageViews with constraints. I have a question about resizing the constraints. How can I set different constraint constants for different devices sizes without programming if/else loops. Is there a way to set it in the storyboard? I have a leading constraint to parent layout for example with an constant value of 10. This is okay for the iPhone 5 screen, but on an iPhone 6/ 6 plus screen it should be higher than 10.

iPhone5 Autolayout

iPhone6 Autolayout

解决方案

Without writing a single line of Code!

Once my junior developer asked me the same question that how can I differentiate between iPhoneSE and iPhone6 for some constraint at that time there was only one solution that was writing some thing like

if device == iPhoneSE { 
   constant = 44 
} else if device == iPhone6 {
   constant = 52
}

To overcome this issue I created a library Layout Helper so now you can update constraint for each device without writing a single line of code.

Step 1

Assign the NSLayoutHelper to your constraint

Step 2

Update the constraint for the device you want

Step 3

Run the app and see the MAGIC

这篇关于如何为不同的屏幕尺寸设置不同的自动布局约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何为不同的屏幕尺寸设置不同的自动布局约束

基础教程推荐