gpt4 book ai didi

ios - 如何正确地在具有内在内容大小的 View 之间进行约束?

转载 作者:行者123 更新时间:2023-11-28 11:34:52 30 4
gpt4 key购买 nike

我在 UIViewUIButton 中有两个 UILabel(垂直方向)在一个“行”中。我需要得到这个结果: enter image description here

查看初始化代码:

private lazy var contentView: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false

return view
}()

private lazy var priceView: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.setContentHuggingPriority(.required, for: .horizontal)

return view
}()

private lazy var priceLabel: UILabel = {
let label = UILabel(frame: .zero)
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false

return label
}()

private lazy var oldPriceLabel: UILabel = {
let label = UILabel(frame: .zero)
label.numberOfLines = 1
label.translatesAutoresizingMaskIntoConstraints = false

return label
}()

private lazy var callButton: UIButton = {
let button = UIButton(frame: .zero)
button.translatesAutoresizingMaskIntoConstraints = false
button.layer.cornerRadius = 22.0
button.setTitleColor(.white, for: .normal)
button.setTitle("Call", for: .normal)
button.setContentHuggingPriority(.defaultLow, for: .horizontal)
button.setContentCompressionResistancePriority(.required, for: .horizontal)

return button
}()

我使用 SnapKit 进行约束:

self.contentView.snp.makeConstraints { make in
make.top.bottom.equalToSuperview()
make.left.equalToSuperview().offset(16.0)
make.right.equalToSuperview().offset(-16.0)
}
self.priceView.snp.makeConstraints { make in
make.centerY.left.equalToSuperview()
make.right.equalTo(self.callButton.snp.left)

}
self.priceLabel.snp.makeConstraints { make in
make.left.equalToSuperview()
make.top.equalToSuperview()
make.right.lessThanOrEqualToSuperview()
}

self.oldPriceLabel.snp.makeConstraints { make in
make.left.bottom.equalToSuperview()
make.top.equalTo(self.priceLabel.snp.bottom).offset(8.0)
make.right.lessThanOrEqualToSuperview()
})
self.callButton.snp.makeConstraints { make in
make.left.equalTo(self.priceView.snp.right)
make.centerY.equalToSuperview()
make.height.equalTo(44.0)
make.right.equalToSuperview()

}

我希望 priceView 缩小到 UILabel 的大小,并且 callButton 在所有可用空间上拉伸(stretch)。拥抱和压缩不起作用,我不明白为什么。

现在: enter image description here

最佳答案

如果您不想使用 UIStackView,您可以轻松解决问题...

对于每个标签,更改:

make.right.lessThanOrEqualToSuperview()

到:

make.right.equalToSuperview()

这应该可以解决问题。

关于ios - 如何正确地在具有内在内容大小的 View 之间进行约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55757540/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com