问题
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.
回答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