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

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

AssigntheNSLayoutHelpertoyourconstraint

Step 2

Updatetheconstraintforthedeviceyouwant

Step 3

RuntheappandseetheMAGIC

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
Dropbox Files.download does not start when number of files in folder is gt; 1000(当文件夹中的文件数为1000时,Dropbox Files.Download不会启动)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)