我正在制作一个自定义列表单元格(集合视图列表单元格),基于this article。我手动添加了单元格中视图的高度,但我在Xcode控制台中看到以下警告,并不确定需要修复哪个部分。
[LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
translatesAutoresizingMaskIntoConstraints)
(
"<NSAutoresizingMaskLayoutConstraint:0x281209220 h=--& v=--& liveTest.LiveChannelContentView:0x128c13430.height == 44 (active)>",
"<NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60 (active)>",
"<NSLayoutConstraint:0x2812372a0 V:|-(0)-[UIView:0x128c136b0] (active, names: '|':liveTest.LiveChannelContentView:0x128c13430 )>",
"<NSLayoutConstraint:0x2812372f0 UIView:0x128c136b0.bottom == liveTest.LiveChannelContentView:0x128c13430.bottom (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60 (active)>
下面的代码是我收到此错误信息的地方。
class LiveChannelContentView: UIView, UIContentView {
let contentsView = UIView()
lazy var titleLabel: UILabel = {
let label = UILabel()
label.text = ""
return label
}()
lazy var statusLabel: UILabel = {
let label = UILabel()
label.text = ""
return label
}()
lazy var symbolImageView: UIImageView = {
let imageView = UIImageView()
imageView.contentMode = .scaleAspectFit
return imageView
}()
var liveEvent: LiveEvent?
init(configuration: LiveChannelContentConfiguration) {
// Custom initializer implementation here.
super.init(frame: .zero)
print("this is the view height: \(self.bounds.height)") // -> I get 0.0 in here
setupAllViews()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupAllViews() {
addSubview(contentsView)
contentsView.addSubview(symbolImageView)
contentsView.addSubview(indicator)
contentsView.addSubview(titleLabel)
contentsView.addSubview(statusLabel)
contentsView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
contentsView.heightAnchor.constraint(equalToConstant: 60)
])
contentsView.backgroundColor = .yellow
symbolImageView.centerY(leading: contentsView.leadingAnchor, trailing: nil, parent: contentsView, paddingLeft: 0, paddingRight: 0, size: CGSize(width: 50, height: 50))
indicator.centerY(leading: contentsView.leadingAnchor, trailing: nil, parent: contentsView, paddingLeft: 0, paddingRight: 0, size: CGSize(width: 50, height: 50))
titleLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
titleLabel.leadingAnchor.constraint(equalTo: symbolImageView.trailingAnchor, constant: 8),
titleLabel.topAnchor.constraint(equalTo: symbolImageView.topAnchor),
titleLabel.trailingAnchor.constraint(equalTo: contentsView.trailingAnchor)
])
statusLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
statusLabel.leadingAnchor.constraint(equalTo: titleLabel.leadingAnchor),
statusLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor),
statusLabel.trailingAnchor.constraint(equalTo: titleLabel.trailingAnchor)
])
print("this is the view after setup: \(self.bounds.height)") // I also get 0.0 in here
}
}
因此,为了澄清LiveChannelContentView的位置,我只需将黄色背景添加到该视图中。 有两件事我不明白。首先,即使Xcode告诉我
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x2812371b0 UIView:0x128c136b0.height == 60 (active)>
当我对应用程序进行屏幕截图并测量黄色背景UIView的高度时,它仍然是60。我认为打破约束意味着使用其他高度约束而不是60,但这样做是错误的吗?
另一件事是我很好奇在我的代码中使用了“<NSAutoresizingMaskLayoutConstraint:0x281209220 h=--& v=--& liveTest.LiveChannelContentView:0x128c13430.height == 44 (active)>”。我在我的工作区中搜索包含
44
的文件,但是什么也没找到。不太确定,但我认为44和60的高度应用于同一个UIView,而Xcode消除了60的高度锚点或类似的东西。然而,当我删除contentsView的高度锚点
contentsView.heightAnchor.constraint(equalToConstant: 60)
时,应用程序会崩溃,如下所示。
我也尝试删除contentsView的顶部或底部锚点,但这也导致了应用程序崩溃。contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
或者
contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
所以,有人能告诉我应该修复哪个约束条件才能消除警告吗?