swift - Swift - 从 UICollectionViewCell 内的视图中删除约束
问题描述
我以编程方式在 custom 内部的视图中添加了一些约束UICollectionViewCell
,但我注意到当一个单元格被重用时,这些约束可能会被重新添加。
这是正在记录的内容:
> Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints
> to catch this in the debugger. The methods in the
> UIConstraintBasedLayoutDebugging category on UIView listed in
> <UIKitCore/UIView.h> may also be helpful. 2020-07-21
> 01:00:10.466637+0300 AutoLayout Message Cells[5227:336361]
> [LayoutConstraints] Unable to simultaneously satisfy constraints.
> Probably at least one of the constraints in the following list is one
> you don't want. Try this: (1) look at each constraint and try to
> figure out which you don't expect; (2) find the code that added the
> unwanted constraint or constraints and fix it. (
> "<NSLayoutConstraint:0x600000435090 AutoLayout_Message_Cells.MessageCollectionViewCell:0x7fbd76e24670.height
> == UIView:0x7fbd76e291b0.height (active)>",
> "<NSLayoutConstraint:0x6000004350e0 V:|-(8)-[UILabel:0x7fbd76e29510'UOA'] (active, names:
> '|':UIView:0x7fbd76e291b0 )>",
> "<NSLayoutConstraint:0x6000004351d0 UILabel:0x7fbd76e29510'UOA'.height == 30 (active)>",
> "<NSLayoutConstraint:0x600000435220 V:[UILabel:0x7fbd76e29510'UOA']-(-12)-[UILabel:0x7fbd76e29790'2h']
> (active)>",
> "<NSLayoutConstraint:0x600000435310 UILabel:0x7fbd76e29790'2h'.height == 30 (active)>",
> "<NSLayoutConstraint:0x6000004353b0 V:[UILabel:0x7fbd76e29790'2h']-(16)-[UITextView:0x7fbd7805b200'Lorem
> ipsum dolor sit ame...'] (active)>",
> "<NSLayoutConstraint:0x600000435400 UITextView:0x7fbd7805b200'Lorem ipsum dolor sit ame...'.bottom ==
> UIView:0x7fbd76e291b0.bottom - 1 (active)>",
> "<NSLayoutConstraint:0x60000042a990 'UIView-Encapsulated-Layout-Height'
> AutoLayout_Message_Cells.MessageCollectionViewCell:0x7fbd76e24670.height
> == 50 (active)>" )
这是我的手机的代码:
import UIKit
class MessageCollectionViewCell: UICollectionViewCell {
@IBOutlet var textView: UITextView!
@IBOutlet var schoolLabel: UILabel!
@IBOutlet var dateLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
setupConstraints()
}
fileprivate func setupConstraints() {
setupCellConstraints()
setupSchoolLabelConstraints()
setupDateLabelConstraints()
setupTextViewConstraints()
}
fileprivate func setupCellConstraints() {
self.contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
self.contentView.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width - (self.frame.origin.x * 2))
])
NSLayoutConstraint.activate([
self.widthAnchor.constraint(equalTo: self.contentView.widthAnchor),
self.heightAnchor.constraint(equalTo: self.contentView.heightAnchor)
])
}
fileprivate func setupSchoolLabelConstraints() {
schoolLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
schoolLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 8),
schoolLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8),
schoolLabel.trailingAnchor.constraint(equalTo: self.contentView.centerXAnchor, constant: 60),
schoolLabel.heightAnchor.constraint(equalToConstant: 30)
])
}
fileprivate func setupDateLabelConstraints() {
dateLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
dateLabel.topAnchor.constraint(equalTo: schoolLabel.bottomAnchor, constant: -12),
dateLabel.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8),
dateLabel.trailingAnchor.constraint(equalTo: self.contentView.centerXAnchor, constant: 60),
dateLabel.heightAnchor.constraint(equalToConstant: 30)
])
}
fileprivate func setupTextViewConstraints() {
textView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
textView.topAnchor.constraint(equalTo: dateLabel.bottomAnchor, constant: 16),
textView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -1),
textView.leadingAnchor.constraint(equalTo: self.contentView.leadingAnchor, constant: 8),
textView.trailingAnchor.constraint(lessThanOrEqualTo: self.contentView.centerXAnchor, constant: 60)
])
}
有没有办法在重新添加之前清除所有约束?
解决方案
我也以编程方式而不是在情节提要中使用约束。您遇到的问题是某些约束不能共存。
例如在代码中我没有看到:
> "<NSLayoutConstraint:0x60000042a990 'UIView-Encapsulated-Layout-Height'
> AutoLayout_Message_Cells.MessageCollectionViewCell:0x7fbd76e24670.height
> == 50 (active)>" )
在某个地方(也许在故事板中?)您给出的高度为 50,但是:
单元格具有与 contentView 相同的高度:
self.heightAnchor.constraint(equalTo: self.contentView.heightAnchor)
在您提供的单元格中:
schoolLabel.topAnchor.constraint(equalTo: self.contentView.topAnchor, constant: 8),
....
schoolLabel.heightAnchor.constraint(equalToConstant: 30)
所以我们有 8 形式的内容顶部 + 30 的高度(38),然后:
dateLabel.topAnchor.constraint(equalTo: schoolLabel.bottomAnchor, constant: -12),
....
dateLabel.heightAnchor.constraint(equalToConstant: 30)
您返回 12,然后再次返回 30 (38 - 12 + 30 = 56) 的高度。我们超出了 50 个单元格,再加上您添加的文本:
textView.topAnchor.constraint(equalTo: dateLabel.bottomAnchor, constant: 16),
textView.bottomAnchor.constraint(equalTo: self.contentView.bottomAnchor, constant: -1)
其高度将导致负数(textView 顶部为 56 + 16 = 72,但底部为 50(我们看不到的约束)- 1 = 49 --> 它的高度为 -23
推荐阅读
- r - 未更改任何内容后未加载文件
- c# - FluentValidation 和自定义消息,告诉用户允许/预期哪些值
- python - 我可以将 Slack 事件 API 与斜杠命令一起使用吗?
- c - 重置日志文件后双重释放或损坏(顶部)
- c# - 为什么 IE 的 selenium webdriver 无法单击具有特定 CSS 的 iframe 中的元素?
- javascript - 在数据中使用特殊字符时,Json 解析会出错
- regex - 正则表达式 - 贪婪直到下一场比赛
- r - 如果模式匹配,则为列名添加前缀
- c++ - 将 C++ DLL 导出到 Visual Basic
- json - extract subset of imported json file in vue's data() function