Bottom-align the text of a sequence of UILabels within a horizontal UIStackView

ε祈祈猫儿з 提交于 2021-01-28 04:37:37

问题


How do I programmatically align the text of a sequence of UILabels within a horizontal UIStackView so that all text is at the bottom of the views?

Below is an image of what I have and what I want. I already know how to create the views, I just can't figure out what properties or constraints are needed to get the text to align on the bottom of the views. Thanks.

top stack view shows what I can already do, bottom shows what I want to achieve


回答1:


Related to what @mahbaleshwar mentioned, UILabels always center align. However, UIStackView has some properties that will help you achieve the view you want, maybe without the yellow backgrounds.

stackView.alignment = bottom will align everything to the bottom.

However, if you need the yellow background, you will need to place your labels in a view. The view will have the yellow background, stackView.alignment = fill will force those views to fill the space. Then, using constraints, you can pin your label to the leading, trailing and bottom edges of the parent view. The label should resize when you assign the text, giving the appearance you want.




回答2:


You can also set a contraint between each UILabel and UIView in InterfaceBuilder too. Like this:



来源:https://stackoverflow.com/questions/43737798/bottom-align-the-text-of-a-sequence-of-uilabels-within-a-horizontal-uistackview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!