【问题标题】:Self-sizing view won't lay out correctly in UIStackView自定尺寸视图在 UIStackView 中无法正确布局
【发布时间】:2021-11-16 07:37:43
【问题描述】:

我有一个包含 UIStackView 的容器组(radio 组),并且该堆栈视图包含一堆可以具有不同大小的项目,具体取决于它们的标签内容。它们有两个标签,可以跨越多行。

很遗憾,我无法正确布置项目的内容。我尝试以许多不同的方式重新排序约束,我尝试将标签内容嵌套到容器视图中,但我似乎无法让堆栈视图的内容正确布局。它总是会下溢并截断标签的最末端,或者它的大小会稍微太小并迫使内容自己挤进框架:

项目的约束是:

NSLayoutConstraint.activate([
   radioView.topAnchor.constraint(equalTo: topAnchor, constant: contentInsets.top)
   radioView.leftAnchor.constraint(equalTo: leftAnchor, constant: contentInsets.left),
   bottomAnchor.constraint(greaterThanOrEqualTo: radioView.bottomAnchor, constant: contentInsets.bottom),
   textLabel.topAnchor.constraint(equalTo: topAnchor, constant: contentInsets.top),
   textLabel.leftAnchor.constraint(equalTo: radioView.rightAnchor, constant: labelsSpacingLeft),
   textLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -contentInsets.right),
   detailTextLabel.topAnchor.constraint(equalTo: textLabel.bottomAnchor, constant: detailTextLabelSpacingTop),
   detailTextLabel.leftAnchor.constraint(equalTo: radioView.rightAnchor, constant: labelsSpacingLeft),
   detailTextLabel.rightAnchor.constraint(lessThanOrEqualTo: rightAnchor, constant: -contentInsets.right),
   bottomAnchor.constraint(greaterThanOrEqualTo: detailTextLabel.bottomAnchor, constant: contentInsets.bottom),
])

【问题讨论】:

    标签: ios swift autolayout nslayoutconstraint uistackview


    【解决方案1】:

    使用this 答案使其正常工作。只需将这些属性应用于标签:

    label.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
    label.setContentHuggingPriority(.defaultHigh, for: .vertical)
    

    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2017-09-07
      • 1970-01-01
      • 2019-07-31
      • 2019-12-04
      • 2015-04-05
      相关资源
      最近更新 更多