【发布时间】:2020-07-20 22:14:34
【问题描述】:
我以编程方式在自定义 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)
])
}
有没有办法在重新添加之前清除所有约束?
【问题讨论】:
-
这是唯一的出路?我不认为保留对约束的引用是最好的......
-
好的,我刚刚查看了您的约束,它们本身就是错误的。
-
我刚刚开始使用 AutoLayout,所以您可能是对的!我做错了什么?
-
只需在storyboard中配置约束即可。并且不要约束单元格或内容视图。删除所有代码。
-
我正在限制单元格的宽度,以便它可以根据
textViews 文本具有动态高度
标签: swift uicollectionviewcell nslayoutconstraint