如何设置两个 UILabels 压缩优先级使一个垂直推动另一个?

How to set two UILabels compression priority to make one push the other vertically?

一图胜千言:

这两个标签在一个 UICollectionviewCell 中。这些是标签上激活的包含:

blue.topAnchor.constraint(equalTo: container.topAnchor)
blue.leftAnchor.constraint(equalTo: container.leftAnchor)
blue.rightAnchor.constraint(equalTo: container.rightAnchor)

green.topAnchor.constraint(equalTo: bottomAnchor)
green.leftAnchor.constraint(equalTo: container.leftAnchor)
green.rightAnchor.constraint(equalTo: container.rightAnchor)
green.bottomAnchor.constraint(equalTo: container.bottomAnchor)

以及我尝试做但似乎不起作用的事情:

blue.setContentHuggingPriority(.defaultLow, for: .vertical)
blue.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
green.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)

如何让蓝色标签被绿色标签推到尽可能少的space?

两个标签都是多行的。

您需要将绿色标签的垂直内容拥抱设置为低于蓝色标签的垂直内容拥抱。

当我忘记它是如何工作的时候,这篇文章总是对我有帮助: https://medium.com/@abhimuralidharan/ios-content-hugging-and-content-compression-resistance-priorities-476fb5828ef