简体   繁体   English

内容拥抱优先级不适用于 UIStackView 中的自定义视图和 label

[英]Content hugging priority not working with custom view and label in UIStackView

I am having trouble getting content hugging working on a custom view.我无法让内容拥抱在自定义视图上工作。 I have the following code:我有以下代码:

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

I add these two views to a stack view:我将这两个视图添加到堆栈视图中:

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

The result is like so:结果是这样的:

在此处输入图像描述

The LIVE NOW view should hug it's content. LIVE NOW 视图应该包含它的内容。 It's defined like so:它是这样定义的:

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")
    }

}

Not sure why it won't hug the content when in a stack view.不知道为什么它在堆栈视图中不会拥抱内容。 Any ideas?有任何想法吗? Would really appreciate some pointers on this.非常感谢对此的一些指示。 thanks!谢谢!

Add these lines in your PillView init(...) func:在您的PillView init(...)函数中添加这些行:

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

You then don't need to set either of those properties for your pillView or dateLabel instances.然后,您不需要为您的pillViewdateLabel实例设置这些属性中的任何一个。

Check the distribution property for stackview.检查 stackview 的分布属性。 It should not be fillEqually as it will ignore the other view's configuration.它不应该是fillEqually ,因为它会忽略其他视图的配置。 Mark its value to fill标记其值以fill

EDIT:编辑:

FillProportionally get me result as: FillProportionally 得到我的结果: 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM