如何将两个UILabel连续放置,以便它们根据iOS中文本的大小垂直居中?

时间:2017-07-31 08:08:43

标签: ios swift uitableview uibutton uilabel

我有一个带有标签的表格行和一个按钮,它们都应根据显示的文本垂直居中。这意味着行的高度应该基于哪个文本是最大的。见下图:

enter image description here

我尝试过将行换行设置为WordWrap并垂直居中两者,但右侧的UIButton不会相应缩放。

约束:

*label* top 5 to contentview
*label* bottom 5 to contentview
*label* leading 5 to contentview
*label* width 0.75 of contentview

*button* leading 5 to label
*button* trailing 5 to contentview
*button* bottom 5 to contentview
*button* top 5 to contentview                                  

3 个答案:

答案 0 :(得分:1)

尝试此设置

正确的标签 Right Label

左标签

Left Label

现在将按钮放在右侧标签上,如enter image description here

答案 1 :(得分:0)

不要在任何视图上设置高度限制。只需设置标签和按钮的顶部和底部约束,以及显着的前导和尾部。

你甚至不需要让它们垂直居中,如果它们具有相同的顶部和底部边距,UITableViewCell将完美地包裹它们。

答案 2 :(得分:0)

在为两个标签设置文字后,您可以获得两个标签的endY,并在height+padding代表内返回相应的tableView