假设我有三个标签在一列中彼此下面排列。 最上面的标签的上边缘固定在superview的上边缘。 所有后续标签的上边缘都固定在前一个标签的下边缘。 所有标签的前缘和后缘都固定在superview的前缘和后缘。 这是它在Interface Builder中的样子(我在每个标签上添加了蓝色背景以显示其范围)。
在模拟器中,结果如下所示。
所有标签都连接到视图控制器中的插座。
@IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var label3: UILabel!底部/顶部间距约束出口
为中间标签的顶部或底部间距约束定义出口。
@IBOutlet weak var spacingConstraint: NSLayoutConstraint!高度约束出口
将中间标签周围的垂直间距设置为零,并将其高度增加相同的量。 定义高度约束的出口并按上述步骤操作,当文本为nil时将高度设置为零,当高度不为nil时将高度设置为初始值。
这仍然像以前的方法一样笨拙。 此外,您必须对间距进行硬编码,并且不能使用内置的默认间距(“接口”构建器中的空白字段)。
UIStackView
这不是一个选项,因为UIStackView仅适用于iOS 9及更高版本。
Suppose I have three labels that are laid out below each other in a column. The uppermost label's top edge is pinned to the superview's top edge. All following labels' top edges are pinned to the preceding label's bottom edge. The leading and trailing edges of all labels are pinned to the leading and trailing edge of the superview. Here's what it looks like in Interface Builder (I added a blue background on every label to visualize its extent).
In the simulator the result looks like this.
All labels are connected to outlets in a view controller.
@IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var label3: UILabel!Bottom/Top Spacing Constraint Outlet
Define an outlet for the middle label's top or bottom spacing constraint.
@IBOutlet weak var spacingConstraint: NSLayoutConstraint!Height Constraint Outlet
Set the vertical spacing around the middle label to zero and increase its height by the same amount. Define an outlet for the height constraint and proceed as above, setting the height to zero when the text is nil and back to it's initial value when the height is not nil.
This is still as clumsy as the previous approach. In addition, you have to hardcode the spacing and cannot use the built-in default spacings (blank fields in Interface builder).
UIStackView
This is not an option since UIStackView is only available on iOS 9 and above.
最满意答案
我正在使用此 UIView类别来实现此目的。
它通过使用objective-c运行时框架添加另外两个名为fd_collapsed和fd_collapsibleConstraints属性来扩展UIView 。 当fd_collapsed属性设置为YES时,您只需拖动要禁用的约束。 在场景后面,它捕获这些约束的初始值,然后在fd_collapsed为YES时设置为零。 当fd_collapsed为NO时,重置为初始值。
还有另一个名为fd_autocollapsed属性
并非每个视图都需要添加宽度或高度约束,像UILabel,UIImageView这样的视图在其中包含内容时具有其内在内容大小。 对于这些视图,我们提供了一个自动折叠属性,当其内容消失时,所选约束将自动折叠。
只要指定的视图包含要显示的内容,此属性就会自动将fd_collapsed属性设置为YES。
它使用起来非常简单。 没有像这样的内置解决方案,这有点遗憾。
I'm using this UIView category for this purpose.
It extends UIView by adding two more property named fd_collapsed and fd_collapsibleConstraints using objective-c runtime framework. You simply drag constraints that you want to be disabled when fd_collapsed property set to YES. Behind the scene, it captures the initial value of these constraints, then set to zero whenever fd_collapsed is YES. Reset to initial values when fd_collapsed is NO.
There is also another property called fd_autocollapsed
Not every view needs to add a width or height constraint, views like UILabel, UIImageView have their Intrinsic content size when they have content in it. For these views, we provide a Auto collapse property, when its content is gone, selected constraints will collapse automatically.
This property automatically sets fd_collapsed property to YES whenever specified view has content to display.
It's really simple to use. It's kinda shame that there is no builtin solution like that.
当文本为零时,如何轻松折叠标签周围的垂直空间?(How to easily collapse vertical space around label when its text is nil?)假设我有三个标签在一列中彼此下面排列。 最上面的标签的上边缘固定在superview的上边缘。 所有后续标签的上边缘都固定在前一个标签的下边缘。 所有标签的前缘和后缘都固定在superview的前缘和后缘。 这是它在Interface Builder中的样子(我在每个标签上添加了蓝色背景以显示其范围)。
在模拟器中,结果如下所示。
所有标签都连接到视图控制器中的插座。
@IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var label3: UILabel!底部/顶部间距约束出口
为中间标签的顶部或底部间距约束定义出口。
@IBOutlet weak var spacingConstraint: NSLayoutConstraint!高度约束出口
将中间标签周围的垂直间距设置为零,并将其高度增加相同的量。 定义高度约束的出口并按上述步骤操作,当文本为nil时将高度设置为零,当高度不为nil时将高度设置为初始值。
这仍然像以前的方法一样笨拙。 此外,您必须对间距进行硬编码,并且不能使用内置的默认间距(“接口”构建器中的空白字段)。
UIStackView
这不是一个选项,因为UIStackView仅适用于iOS 9及更高版本。
Suppose I have three labels that are laid out below each other in a column. The uppermost label's top edge is pinned to the superview's top edge. All following labels' top edges are pinned to the preceding label's bottom edge. The leading and trailing edges of all labels are pinned to the leading and trailing edge of the superview. Here's what it looks like in Interface Builder (I added a blue background on every label to visualize its extent).
In the simulator the result looks like this.
All labels are connected to outlets in a view controller.
@IBOutlet weak var label1: UILabel! @IBOutlet weak var label2: UILabel! @IBOutlet weak var label3: UILabel!Bottom/Top Spacing Constraint Outlet
Define an outlet for the middle label's top or bottom spacing constraint.
@IBOutlet weak var spacingConstraint: NSLayoutConstraint!Height Constraint Outlet
Set the vertical spacing around the middle label to zero and increase its height by the same amount. Define an outlet for the height constraint and proceed as above, setting the height to zero when the text is nil and back to it's initial value when the height is not nil.
This is still as clumsy as the previous approach. In addition, you have to hardcode the spacing and cannot use the built-in default spacings (blank fields in Interface builder).
UIStackView
This is not an option since UIStackView is only available on iOS 9 and above.
最满意答案
我正在使用此 UIView类别来实现此目的。
它通过使用objective-c运行时框架添加另外两个名为fd_collapsed和fd_collapsibleConstraints属性来扩展UIView 。 当fd_collapsed属性设置为YES时,您只需拖动要禁用的约束。 在场景后面,它捕获这些约束的初始值,然后在fd_collapsed为YES时设置为零。 当fd_collapsed为NO时,重置为初始值。
还有另一个名为fd_autocollapsed属性
并非每个视图都需要添加宽度或高度约束,像UILabel,UIImageView这样的视图在其中包含内容时具有其内在内容大小。 对于这些视图,我们提供了一个自动折叠属性,当其内容消失时,所选约束将自动折叠。
只要指定的视图包含要显示的内容,此属性就会自动将fd_collapsed属性设置为YES。
它使用起来非常简单。 没有像这样的内置解决方案,这有点遗憾。
I'm using this UIView category for this purpose.
It extends UIView by adding two more property named fd_collapsed and fd_collapsibleConstraints using objective-c runtime framework. You simply drag constraints that you want to be disabled when fd_collapsed property set to YES. Behind the scene, it captures the initial value of these constraints, then set to zero whenever fd_collapsed is YES. Reset to initial values when fd_collapsed is NO.
There is also another property called fd_autocollapsed
Not every view needs to add a width or height constraint, views like UILabel, UIImageView have their Intrinsic content size when they have content in it. For these views, we provide a Auto collapse property, when its content is gone, selected constraints will collapse automatically.
This property automatically sets fd_collapsed property to YES whenever specified view has content to display.
It's really simple to use. It's kinda shame that there is no builtin solution like that.
发布评论