将尝试通过打破约束来恢复(但不确定为什么我的约束是错误的)
Will attempt to recover by breaking constraint (but not sure why my constraints are wrong)
我正在制作一个基于 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)>"
is used in my code. I searched the file contains 44
in my workspace but I got nothing.
不太确定,但我认为 44 和 60 的高度应用于相同的 UIView 并且 Xcode 摆脱了 60 的高度锚点或其他东西。但是,当我删除 contentsView 的高度锚点时,contentsView.heightAnchor.constraint(equalToConstant: 60)
,应用程序崩溃如下。
我也试过删除 contentsView 的顶部或底部锚点,但它也破坏了应用程序。
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
或
contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
所以谁能告诉我我应该修复哪个约束以消除警告?
更改此部分
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.translatesAutoresizingMaskIntoConstraints = false
let con = contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
con.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
con,
contentsView.heightAnchor.constraint(equalToConstant: 60)
])
我正在制作一个基于 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)>" is used in my code. I searched the file contains
44
in my workspace but I got nothing.
不太确定,但我认为 44 和 60 的高度应用于相同的 UIView 并且 Xcode 摆脱了 60 的高度锚点或其他东西。但是,当我删除 contentsView 的高度锚点时,contentsView.heightAnchor.constraint(equalToConstant: 60)
,应用程序崩溃如下。
我也试过删除 contentsView 的顶部或底部锚点,但它也破坏了应用程序。
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
或
contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor),
所以谁能告诉我我应该修复哪个约束以消除警告?
更改此部分
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.translatesAutoresizingMaskIntoConstraints = false
let con = contentsView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
con.priority = UILayoutPriority(rawValue: 999)
NSLayoutConstraint.activate([
contentsView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
contentsView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
contentsView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor),
con,
contentsView.heightAnchor.constraint(equalToConstant: 60)
])