动态高度标签和文本字段 [英] Dynamic height label and Text field

查看:63
本文介绍了动态高度标签和文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的图片中,它们具有以下约束:

In the picture below they have these constraints:

我对约束4-7的工作方式感到困惑.该配方声称,它会在运行时根据最高控件(高度)动态设置控件(名称标签和名称文本视图)的垂直间距.因此,如果标签的固有高度为48(由于增加了字体),则textview的高度也将与名称标签的高度匹配.我在代码中的任何地方都看不到这种关系,例如: nameTextView.height = nameLabel.height + 0 ?

I am confused how constraints 4-7 work. This recipe claims that it dynamically sets the vertical spacing of the controls (name label and name text view) based on the tallest control (height wise) at runtime. So if the label has an intrinsic height of 48 (due to increased font) somehow the textview's height will also match the name label's height. I don't see that relationship anywhere in code something like: nameTextView.height = nameLabel.height + 0?

在约束4中,它也可能是> =,但在约束5中,它必须等于(具有较低的优先级).系统如何决定在什么时候使用哪个约束?

Also in constraint 4 it's saying it couldbe >= but in constraint 5 it's saying it has to be equal to (with a lower priority). How does the system decide which constraint to use at what time?

来源: https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/ViewswithIntrinsicContentSize.html#//apple_ref/doc/uid/TP40010853-CH13-SW16

推荐答案

约束5和7在那里定义了接口的默认值,因为约束4和6是不等式并且接受不止一种可能性.

Constraints 5 and 7 are there to define default values for the interface as constraints 4 and 6 are inequalities and accepts more than one possibility.

在此示例中,他们从未说过 nameTextView nameLabel 一定是相同的高度.这四个约束实际上是在照顾它们的不同高度.

In this example they never say that nameTextView and nameLabel would be necessarily the same height. These four constraints are actually taking care of their different heights.

这就是它们的工作方式:

So here is how they work:

约束4和6定义从顶部到 nameLabel nameTextView 的距离可以(等于此术语)等于或大于20.因此,如果您只设置了这两个,如果Xcode实际上大于或等于Xcode将会丢失,这就是为什么您需要具有低优先级约束(5和7)的原因,它们为Xcode提供了指导,即在低优先级时它们都将开始在20岁时,该程序将尝试通过检查谁的身高来满足4岁和6岁(因为4岁和6岁承认20岁或更大):​​ nameLabel nameTextView .如果 nameLabel 较高,则通过将其垂直距离设置为20来满足约束4,而通过将 nameTextView 到顶部的垂直距离设置为大于20(自动)来满足约束6.由它们的高度差计算得出).现在,一切都满足了,自动布局将成功!

Constraints 4 and 6 define that the distance from top to nameLabel and nameTextView, respectively, CAN BE (attention on this term) equal or greater than 20. So if you set only these two, Xcode will be lost if they are actually greater or equal, that's why you need to have the low priority constraints (5 and 7), they give the guideline to Xcode that in low priority they'll both "start" at 20, then the program tries to satisfy 4 and 6, by checking who's taller (because 4 and 6 admits 20 or GREATER): nameLabel or nameTextView. If nameLabel is taller, constraint 4 is satisfied by setting its vertical distance to 20 and constraint 6 will be satisfied by setting nameTextView vertical distance to the top as GREATER than 20 (automatically calculated by the difference of their height). Now, with everything satisfied, autolayout will succeed!

希望这会有所帮助.

这篇关于动态高度标签和文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆