【问题标题】:How to set two UILabels compression priority to make one push the other vertically?如何设置两个 UILabel 压缩优先级以使一个垂直推动另一个?
【发布时间】:2020-08-07 09:45:15
【问题描述】:

一张图胜一千字:

这两个标签在一个 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)

我怎样才能让蓝色标签占用尽可能少的空间,被绿色标签推动?

两个标签都是多行的。

【问题讨论】:

    标签: ios swift nslayoutconstraint


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-08
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      相关资源
      最近更新 更多