【问题标题】:Strange Behaviour of Constraints added Programatically in UITableViewCell | Swift以编程方式在 UITableViewCell 中添加的约束的奇怪行为迅速
【发布时间】:2021-05-02 04:37:10
【问题描述】:

我有一个tableViewCell,我以编程方式对其进行约束。我只有一个 ImageView 在单元格内。现在我没有给单元格的contentView 一个固定的高度,而是给imageView 顶部的topbottom 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),

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

"<0x6000034a4e60 v:><0x6000034a4f00 uiimageview:0x7fa7f6605640.height="="><0x6000034a4f50 uiimageview:0x7fa7f6605640.bottom="=" paigham.messagestableviewcell:0x7fa7f7809c00><0x600003494000 paigham.messagestableviewcell:0x7fa7f7809c00>
<0x6000034a4f00 uiimageview:0x7fa7f6605640.height="=">

&gt;&gt;&gt;&gt;

标签: ios swift uitableview constraints


【解决方案1】:

编辑:

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

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

【讨论】:

  • 是的,我已经尝试将我的子视图添加到 contentView 中,但得到了相同的结果。
  • 能否分享您的自定义单元格代码以及控制器的tableview 委托方法?
  • @AbuBäkr 约束的问题仅在终端中或可见?
  • 只在终端哥!视觉上的 tableview 看起来很棒!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-24
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多