内容拥抱优先级不适用于 UIStackView 中的自定义视图和标签
Content hugging priority not working with custom view and label in UIStackView
我在自定义视图上使用内容拥抱时遇到问题。我有以下代码:
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")
}
}
不确定为什么它在堆栈视图中不包含内容。有任何想法吗?真的很感激这方面的一些建议。谢谢!
检查 stackview 的分布 属性。
它不应该是 fillEqually
因为它会忽略其他视图的配置。
将其值标记为 fill
编辑:
FillProportionally 得到的结果为:
将这些行添加到您的 PillView
init(...)
func:
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)
然后您不需要为您的 pillView
或 dateLabel
实例设置这些属性中的任何一个。
我在自定义视图上使用内容拥抱时遇到问题。我有以下代码:
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")
}
}
不确定为什么它在堆栈视图中不包含内容。有任何想法吗?真的很感激这方面的一些建议。谢谢!
检查 stackview 的分布 属性。
它不应该是 fillEqually
因为它会忽略其他视图的配置。
将其值标记为 fill
编辑:
FillProportionally 得到的结果为:
将这些行添加到您的 PillView
init(...)
func:
label.setContentHuggingPriority(.required, for: .horizontal)
label.setContentCompressionResistancePriority(.required, for: .horizontal)
然后您不需要为您的 pillView
或 dateLabel
实例设置这些属性中的任何一个。