gpt4 book ai didi

ios - 将尝试通过打破约束来恢复(但不确定为什么我的约束是错误的)

转载 作者:行者123 更新时间:2023-12-05 03:34:51 24 4
gpt4 key购买 nike

我正在制作一个自定义列表单元格( Collection View 列表单元格),基于 this article .我在单元格中手动添加 View 的高度,但我在 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
}
}

enter image description here

因此,为了阐明 LiveChannelContentView 的位置,我只是将黄色背景添加到 View 中。有两件事我不明白。首先,甚至 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 的高度 anchor 或其他东西。但是,当我删除 contentsView 的高度 anchor contentsView.heightAnchor.constraint(equalToConstant: 60) 时,应用程序崩溃如下。

enter image description here

我也尝试删除 contentsView 的顶部或底部 anchor ,但它也破坏了应用程序。

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)
])

关于ios - 将尝试通过打破约束来恢复(但不确定为什么我的约束是错误的),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70053681/

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