这只是一个关于如何在项目中快速使用"Variable Characterns"来为iPad和iPhone添加不同布局的扩展.
请阅读本文以了解更多有关班级大小的信息.
https://developer.apple.com/reference/uikit/uitraitcollection个
个
如果您跳过下面的示例,请务必阅读结尾处的摘要.
You need a button having different widths in iPhone and iPad. The former having width of 80 and latter having a width of 300.
Vary for Traits with Multiple Constraints as installed.个
STEPS :个
- 首先添加常见约束,如水平和垂直居中按钮.
- Select VaryForTraits,对于iPhone屏幕,根据尺寸等级指南,C*R尺寸等级适合该型号,我们判断宽度和;弹出窗口中的高度.点击屏幕上的任意位置,关闭弹出窗口.
- 添加宽度常量,并判断是否为C*R大小类添加了约束.添加约束后, Select "完成"按钮.
- 对于iPad屏幕,再次 Select 任何iPad设备并 Select VaryForTraits,这一次单击Height-Width时,应该会显示R*R Variation.
- 再次添加宽度约束,最后添加的iPhone宽度约束必须取消高亮显示,如截图所示.这次增加的值将用于尺寸级R*R.
个
- 切换回iPhone的布局,宽度为80,iPad的宽度为300.
个
CONCLUSION :个个
请注意,总共添加了两个约束,在这两个约束中,值根据所选的大小类别而不同.
Vary for Traits with Single Constraint , Multiple Size-Class installed个
- STEPS :个
- 添加法线宽度约束.然后 Select 该约束并 Select 常量值旁边的+按钮.
个
- 添加特征变量,对于iPhone,我们 Select C*R并将常量值设置为100.
个
- 同样,对于遵循特征变量R*R的iPad,我们通过再次点击+按钮添加另一个变量,并将值设置为300.
个
- Select 一台iPad,宽度会自动取为300,回到iPhone,取值为100.
CONCLUSION :个个
当只需要一个约束且常量值不同时,这似乎是一个更好的 Select ,而不是添加两个约束.
WHEN TO USE, WHAT TO USE :个
这两种方法基本上都在做相同的事情,都是将值设置为Size-Class.
但是,当您想要专门为设备或大小类添加约束时,可以使用#Method1.例如,在iPhone中,按钮应该位于前50个点,而在iPad中,它应该在水平和垂直方向上居中.在这种情况下,需要使用VaryForTraits,因为它会打开门,以便为特定尺寸类别添加约束.
如果希望同一约束类型具有不同的常量值,则使用#Method2.
P.S : TO ALL THOSE WHO ARE NOT ABLE TO GET THE EXAMPLE WORKING个
Please make sure, you are adding only the required constraints as Installed. The checkbox against Installed should only appear for the constraint that you need for a size class. That is the key!个
Just add a top constraint & a leading to an uiButton in a view. Select the top constraint and uncheck the basic Installed option with Plus sign. Now, by clicking on the Plus sign, add variation to CR并选中该选项.现在,用不同的方向组合将设备从iPhone换成iPad.此约束仅适用于CR size class which is iPhone in portrait orientation. If the checkbox against the basic Installed (the one with Plus symbol) was checked that means the constraint should be applied to all size classes.
SUMMARY :个
用户界面的Trait Variation is a change to the presentation of your user interface that is based on a device configuration.种特性变化不仅限于约束,还可以应用到更多方面.例如,当设备设置为深色样式时,更改背景和其他元素的 colored颜色 .变体可以应用于用户界面的元素,例如删除约束,也可以应用于视图类或约束的属性,例如标签的字体.你可以改变:
视图的大小或位置
安装视图
安装约束
约束常数
字体
字体、色调或背景的 colored颜色
布局页边距
图像文件
The specific set of properties you can vary depends on the class of the element. In the example, we have demonstrated the use of- 安装约束 & - 约束常数 . Others, are quite simple and can be easily inferred.