我想在按钮旁边布置标签,如下所示:
[--[UILabel]-[UIButton]--]
我希望它们在superview中居中,考虑到UILabel和UIButton具有动态文本长度。
如何使用AutoLayout或UIStackViews?
谢谢,
答案 0 :(得分:2)
答案 1 :(得分:1)
将标签和按钮对齐到superview。如果您正在使用故事板或XIB文件,请选择标签Horizontally Centered Constraint
,然后在尺寸检查器中(在Xcode的右栏,第5项)选择Label.Trailing
而不是{{1 }}。并给它一个值(或零,无论你想要什么。这将是标签和视图中心之间的常量)。
设置后,Label.X
中标签的Center X Alignment Constraint
应如下所示:
对按钮执行相同操作。但是这一次,选择Size Inspector
而不是Button.Leading
并给它一个常量。
设置后,Button.X
中按钮的Leading Alignment Constraint
应如下所示:
通过此解决方案,Label和Button将彼此独立。您可以单独决定其他属性。
答案 2 :(得分:0)
在StackView中放置标签和按钮,并为StackView提供约束(中心水平,中心垂直)到superview
答案 3 :(得分:-1)