ios - UITableView 单元格约束问题 - Swift

标签 ios swift uitableview

我开始遇到这个约束错误:

"<NSLayoutConstraint:0x6000014419a0 V:|-(20)-[UIImageView:0x7f809779fae0]   (active, names: '|':mChat.ChatCell:0x7f8097534600'ChatCell' )>",
    "<NSLayoutConstraint:0x600001441b30 UIImageView:0x7f809779fae0.bottom == mChat.ChatCell:0x7f8097534600'ChatCell'.bottom - 20   (active)>",
    "<NSLayoutConstraint:0x600001440eb0 UIImageView:0x7f809779fae0.height == 150   (active)>",
    "<NSLayoutConstraint:0x600001440550 'UIView-Encapsulated-Layout-Height' mChat.ChatCell:0x7f8097534600'ChatCell'.height == 60.5   (active)>"

当 UITableView 中至少有两个图像时出现问题,然后我得到这些约束错误。或者当我将另一个图像添加到 UITableView 时。 这是我的约束条件:

func setupMediaMessage(){

        mediaMessage.translatesAutoresizingMaskIntoConstraints = false
        mediaMessage.contentMode = .scaleAspectFill
        mediaMessage.backgroundColor = .lightGray
        mediaMessage.layer.cornerRadius = 16
        mediaMessage.layer.masksToBounds = true
        let constraints = [
            mediaMessage.topAnchor.constraint(equalTo: topAnchor, constant: 20),
            mediaMessage.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),
            mediaMessage.widthAnchor.constraint(equalToConstant: 150),
            mediaMessage.heightAnchor.constraint(equalToConstant: 150)
        ]
        outcomingConstraint = mediaMessage.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 24)
        incomingConstraint = mediaMessage.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -24)
        NSLayoutConstraint.activate(constraints)
}

不要担心尾随和前导约束,我只是检查这些图像是传入还是传出。 我的 Github repo :https://github.com/realpaliy/mChat/blob/master/mChat/Controllers/Chats/ChatCell.swift .所以,UITableView 看起来不错,但为什么会出现这些错误? SH

最佳答案

如果布局看起来正确,则冲突可能是由于内部自动布局引擎和约束评估的顺序造成的。

试试这个——它应该去掉警告,而不改变最终结果:

    let bAnchor = mediaMessage.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20)
    bAnchor.priority = UILayoutPriority(rawValue: 999)
    let constraints = [
        mediaMessage.topAnchor.constraint(equalTo: topAnchor, constant: 20),

        //mediaMessage.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -20),
        bAnchor,

        mediaMessage.widthAnchor.constraint(equalToConstant: 150),
        mediaMessage.heightAnchor.constraint(equalToConstant: 150)
    ]

编辑

注意:我不为 Apple 工作 - 这些只是我的观察:

当单元格被实例化和/或出队时,自动布局使用当前行高来布置单元格内容。然后它使用对单元格内容的约束来确定实际的行高。如果行的高度不足以生成高度,这可能会导致冲突警告。通过将最底部约束(或者,我认为,显式元素高度约束)的优先级更改为 999,这允许自动布局首先打破约束,然后重新执行它。 .没有生成调试警告。

您应该(可能?)还可以通过为 tableView.estimatedRowHeight 设置足够大的值来消除警告。

关于ios - UITableView 单元格约束问题 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59056785/

相关文章:

ios - 圆形图像扭曲与自动布局

ios - 如何在 objective-c 中获得特定高度的字符串

iOS - 具有多个 UITableView 的水平 UIScrollView

ios - 具有自动尺寸的嵌套 UITableView

ios - 重新加载 UITableView 上未显示的数据

IOS 模拟器:JavaScriptCoreWTF::CrashOnOverflow::crash:

ios - 如何在 tableViewCell 中保存 UIButton 的状态?

swift - 如何将 Storyboard中堆栈 View 中的内容居中?

ios - 在同一个 Storyboard 中重用另一个表 VC 中的单元格

json - 下载/解析后在 swift 中打印 json