在UITableViewCell | Swift中编程添加的约束的奇怪行为



我有一个tableViewCell,我以编程方式给予约束。细胞内只有一个ImageView。现在我没有给细胞的contentView一个固定的高度,而是给top一个固定的高度。bottom的imageView顶部contentView,使单元格的高度随着imageView高度的增加而增加。

messagingPersonProfilePicture.leadingAnchor.constraint(equalTo: self.leadingAnchor),
messagingPersonProfilePicture.topAnchor.constraint(equalTo: self.topAnchor, constant: 15),
messagingPersonProfilePicture.widthAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.heightAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.bottomAnchor.constraint(equalTo: self.bottomAnchor, constant: -15),

现在,给定这些约束条件,程序打破了这些约束条件,告诉我以下内容:

" NSLayoutConstraint:0x6000034a4e60 V:|-(15)-[UIImageView:0x7fa7f6605640] (active, names: '|':Paigham.)MessagesTableViewCell: 0 x7fa7f7809c00'tablecellid"one_answers"),,"& lt NSLayoutConstraint: 0 x6000034a4f00 UIImageView: 0 x7fa7f6605640。身高== 50(已激活)>"& lt NSLayoutConstraint: 0 x6000034a4f50 UIImageView: 0 x7fa7f6605640。bottom == paigham . messagstableviewcell:0x7fa7f7809c00'tableCellID'。底部- 15(活跃)>"NSLayoutConstraint:0x600003494000 ' uiview - encapsulation - layout - height ' paighamm . messagestableviewcell:0x7fa7f7809c00'tableCellID'。height == 80.5 (active)>">
将尝试打破约束恢复& lt; NSLayoutConstraint: 0 x6000034a4f00 UIImageView: 0 x7fa7f6605640。

我认为这有点奇怪因为tableviewcell从它的子视图中获得适当的高度。但是一旦我将tableViewseparatorStyle设置为none,约束不再被打破。这背后的原因是什么呢?

我tableViewMethods:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: tableCellID, for: indexPath) as! MessagesTableViewCell
cell.backgroundColor = .blue
return cell
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}

我customCellCode:

class MessagesTableViewCell: UITableViewCell {
var messagingPersonProfilePicture: UIImageView = {
let v = UIImageView()
v.translatesAutoresizingMaskIntoConstraints = false
v.contentMode = .scaleAspectFill
v.image = #imageLiteral(resourceName: "unnamed-3")
v.layer.cornerRadius = 25
v.clipsToBounds = true
v.backgroundColor = .yellow
return v
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .default, reuseIdentifier: reuseIdentifier)
setupMessageCell()
}
func setupMessageCell() {
contentView.addSubview(messagingPersonProfilePicture)

NSLayoutConstraint.activate([
messagingPersonProfilePicture.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
messagingPersonProfilePicture.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 15),
messagingPersonProfilePicture.widthAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.heightAnchor.constraint(equalToConstant: 50),
messagingPersonProfilePicture.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -15),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")

Edit:

在您提供有关该问题的更多信息之后,似乎您的heightAnchor定义与TableView默认单元格高度定义相冲突。发生冲突是因为当UITableView首次加载/被布局时,它将其单元格的高度设置为0。这显然与你自己的约束相冲突(将高度设置为50 + 15顶部+ 15底部)。要解决这个问题,可以定义优先级较低的顶部或底部锚。

let bottomConstraint = messagingPersonProfilePicture.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: -15)
bottomConstraint.priority = .defaultHigh
bottomConstraint.isActive = true

最新更新