【问题标题】:Content hugging priority not working with custom view and label in UIStackView内容拥抱优先级不适用于 UIStackView 中的自定义视图和标签
【发布时间】:2021-03-21 12:49:47
【问题描述】:

我无法在自定义视图上获取内容拥抱。我有以下代码:

pillView.setContentHuggingPriority(.required, for: .horizontal)
pillView.setContentCompressionResistancePriority(.required, for: .horizontal)
dateLabel.setContentHuggingPriority(.defaultLow, for: .horizontal)
dateLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)

我将这两个视图添加到堆栈视图中:

let dateStackView = UIStackView(arrangedSubviews: [pillView, dateLabel])

结果是这样的:

LIVE NOW 视图应该包含它的内容。它是这样定义的:

final class PillView: UIView {

    private enum Constants {
        static let radius: CGFloat = 4.0
        static let labelInsets = UIEdgeInsets(horizontal: 8.0, vertical: 4.0)
    }

    enum Config {
        case attention

        var font: UIFont {
            switch self {
            case .attention: return Font.caption
            }
        }
        var textColor: UIColor {
            switch self {
            case .attention: return .white
            }
        }
        var backgroundColor: UIColor {
            switch self {
            case .attention: return Theme.red100
            }
        }
    }

    // MARK: - Properties

    private let config: Config

    // MARK: - Initializers

    init(text: String, config: Config = .attention) {
        self.config = config
        super.init(frame: .zero)

        backgroundColor = config.backgroundColor
        clipsToBounds = true
        layer.cornerRadius = Constants.radius

        let label = UILabel()
        label.font = config.font
        label.textColor = config.textColor
        label.text = text

        addSubview(label)
        label.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
          label.topAnchor.constraint(equalTo: topAnchor, constant: Constants.labelInsets.top),
          label.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -Constants.labelInsets.bottom),
          label.leadingAnchor.constraint(equalTo: leadingAnchor, constant: Constants.labelInsets.left),
          label.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -Constants.labelInsets.right)
        ])
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

不确定为什么在堆栈视图中它不会拥抱内容。有任何想法吗?非常感谢对此的一些指示。谢谢!

【问题讨论】:

  • pin(to:insets:) 是做什么的?你能展示它的声明吗?
  • 抱歉,这是一个添加约束的扩展。我更新了问题。
  • 由于某种原因,我无法重现此行为...红色标签符合我的预期。
  • @Sweeper 你能分享你的代码吗?
  • Codeoutput。请注意,红色标签不抵抗内容压缩。

标签: ios swift uiview uilabel uistackview


【解决方案1】:

检查 stackview 的分布属性。 它不应该是fillEqually,因为它会忽略其他视图的配置。 将其值标记为fill

编辑:

FillProportionally 让我得到如下结果:

【讨论】:

  • 它是fillfill 是默认值。
  • @Kex 使用 fillProportionally 尝试一次
  • 这不是拥抱第一个标签
  • @Kex 你可以尝试让stackview的尾随约束大于等于而不是euqal吗?这将允许stackview根据内容缩小或扩展?
【解决方案2】:

在您的PillView init(...) func 中添加这些行:

label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)

然后,您无需为 pillViewdateLabel 实例设置这些属性。

【讨论】:

    猜你喜欢
    • 2018-12-01
    • 1970-01-01
    • 2023-02-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-09
    • 2021-01-08
    • 2020-12-01
    • 1970-01-01
    相关资源
    最近更新 更多