我使用的是AutoLayout和Size类,但随着iOS 10和新的Xcode 8.0的发布,有一个新选项Vary for Traits.这是对不同宽度和高度的设备尺寸等级的替代.

enter image description here

选中width复选框后,将显示varying 14 compact width devices.

enter image description here

选中height复选框后,将显示varying 18 compact height devices.

enter image description here

通过选中这两个复选框,它将显示varying 11 compact width regular height devices.

enter image description here

如何利用这个选项? 我们可以像Xcode7.0那样对Size类使用AutoLayout吗? 如果有人有深入的知识,请解释一下.

推荐答案

这只是一个关于如何在项目中快速使用"Variable Characterns"来为iPad和iPhone添加不同布局的扩展.

请阅读本文以了解更多有关班级大小的信息.

https://developer.apple.com/reference/uikit/uitraitcollection

enter image description here

如果您跳过下面的示例,请务必阅读结尾处的摘要.


  • OBJECTIVE :

You need a button having different widths in iPhone and iPad. The former having width of 80 and latter having a width of 300.

  • METHOD 1 :

Vary for Traits with Multiple Constraints as installed.

  • STEPS :

    1. 首先添加常见约束,如水平和垂直居中按钮.

enter image description here

  1. Select VaryForTraits,对于iPhone屏幕,根据尺寸等级指南,C*R尺寸等级适合该型号,我们判断宽度和;弹出窗口中的高度.点击屏幕上的任意位置,关闭弹出窗口.

enter image description here

  1. 添加宽度常量,并判断是否为C*R大小类添加了约束.添加约束后, Select "完成"按钮.

enter image description here

  1. 对于iPad屏幕,再次 Select 任何iPad设备并 Select VaryForTraits,这一次单击Height-Width时,应该会显示R*R Variation.

enter image description here

  1. 再次添加宽度约束,最后添加的iPhone宽度约束必须取消高亮显示,如截图所示.这次增加的值将用于尺寸级R*R.

enter image description hereenter image description here

  1. 切换回iPhone的布局,宽度为80,iPad的宽度为300.

enter image description here

CONCLUSION :个个

请注意,总共添加了两个约束,在这两个约束中,值根据所选的大小类别而不同.


  • METHOD 2 :

Vary for Traits with Single Constraint , Multiple Size-Class installed

  • STEPS :
    1. 添加法线宽度约束.然后 Select 该约束并 Select 常量值旁边的+按钮.

enter image description here

  1. 添加特征变量,对于iPhone,我们 Select C*R并将常量值设置为100.

enter image description hereenter image description here

  1. 同样,对于遵循特征变量R*R的iPad,我们通过再次点击+按钮添加另一个变量,并将值设置为300.

enter image description hereenter image description here

  1. Select 一台iPad,宽度会自动取为300,回到iPhone,取值为100.

enter image description here

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!

enter image description here

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.

Ios相关问答推荐

如何在SwiftUI中绑定文本字段,同时仍使用它来存储Collection 夹?

如何使视图完全适合屏幕,而不会在旋转时溢出手机屏幕的边界?

在工作表上移动图像SwiftUI

在金属中设置纹理的UV坐标以编程方式变形纹理

AVFoundation Camera推出变焦SWIFT

objc[25442]:Swift类的扩展和类别不允许有+load方法

如何在 Android 上的 Flutter 应用中录制内部音频?

表视图(SwiftUI)中的多列?

更新 flutter 和 xcode 后 Xcode 14.3 中缺少文件 (libarclite_iphoneos.a)

工具栏底部 iOS 16 - SwiftUI

在 Swift 中使用 UITesting 捕获屏幕录像

List touch Tabbar 时 SwiftUI Tabbar 消失

在视图控制器中实例化视图控制器是否可能/智能?

来自 c++ 的runtime_error未在 iOS 中捕获

在 UICollectionView 上动态设置布局导致莫名其妙的 contentOffset 变化

滑动列表项以获取更多选项(Flutter)

改变 UIView 位置的简单方法?

UIView 和 initWithFrame 以及一个 NIB 文件.如何加载 NIB 文件?

使用 swift 从应用程序委托打开视图控制器

是否可以将 Socket.io 与 AWS Lambda 一起使用?