使用自动布局将 3 个按钮与 objective C 中的动态内容对齐

Aligning 3 buttons with dynamic contents in objective C using Autolayout

我有 3 个按钮,其中一个低于 other.The 要求,所有按钮都应该集中 aligned.Also 动态内容应该适合这 3 个 buttons.Please 帮我解决这个问题。我对content hugging, compression resistance和intrinsic content只有一个模糊的了解size.Thank你很提前。

将所有 UIBUtton 嵌入 UIStackView 并根据需要设置 stackViews 属性。

要了解这个想法,请参阅

注意:UIStackView 仅在 iOS >= 9 下可用。

对于 iOS 9 及更高版本:

使用 UIStackView 嵌入所有 buttons 并将 stack view 设置在 view 的中心。对于动态内容,设置 stackView's heightConstraint 大于等于 relation (>=).

对于 iOS 8 及更早版本:

只需使用 UIView 而不是 UIStackView。其他的都一样。