Swift: UITableView 分隔线打破了布局约束

Swift: UITableView Separator lines break layout constraints

所以我有以下创建 TableView 的代码:

    contentView.addSubview(carsTableView)
    carsTableView.translatesAutoresizingMaskIntoConstraints = false
    carsTableView.backgroundColor = r.darkGray
    carsTableView.dataSource = self
    carsTableView.isScrollEnabled = false
    carsTableView.allowsSelection = true
    carsTableView.delegate = self
    
    carsTableView.separatorStyle = UITableViewCell.SeparatorStyle.singleLine
    carsTableView.separatorColor = r.lightGray
    carsTableView.separatorInset = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
    
    carsTableView.register(CarCell.self, forCellReuseIdentifier: "CarCell")

请注意,我使用了单行分隔符。以下代码是我的 Cell“CarCell”

的约束
NSLayoutConstraint.activate([
        carView.heightAnchor.constraint(equalToConstant: 120),
        carView.topAnchor.constraint(equalTo: contentView.topAnchor),
        carView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        carView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        carView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
        
        carImageView.leadingAnchor.constraint(equalTo: carView.leadingAnchor, constant: 15),
        carImageView.centerYAnchor.constraint(equalTo: carView.centerYAnchor),
        carImageView.heightAnchor.constraint(equalToConstant: carImageWidthAndHeight),
        carImageView.widthAnchor.constraint(equalToConstant: carImageWidthAndHeight),
        
        carImage.widthAnchor.constraint(equalTo: carImageView.widthAnchor),
        carImage.heightAnchor.constraint(equalTo: carImageView.heightAnchor),
        carImage.centerYAnchor.constraint(equalTo: carImageView.centerYAnchor),
        carImage.centerXAnchor.constraint(equalTo: carImageView.centerXAnchor),
        
        stringStack.leadingAnchor.constraint(equalTo: carImageView.trailingAnchor, constant: 20),
        stringStack.centerYAnchor.constraint(equalTo: carView.centerYAnchor),
        
        editButton.trailingAnchor.constraint(equalTo: carView.trailingAnchor, constant: -15),
        editButton.centerYAnchor.constraint(equalTo: carView.centerYAnchor),
        
    ])

结果是一个完美的 TableView,它看起来完全像它应该的那样。但是,分隔线打破了从 120 到 120.33 的高度限制。我怎样才能避免这种情况?老实说,我很惊讶似乎没有人遇到类似的问题。我该如何解决?差异显然不明显,但约束问题在控制台中困扰着我。我想要它 运行 而不会抛出错误...

这是一个与 UIKit 布局 table 视图和单元格的方式相关的常见问题。

您看到的是 120 to 120.3333,但如果您 运行 在具有 @2x 屏幕比例的设备上使用该应用程序,消息将为 120.5

计算“单像素”线时,@2x设备只能使用整点或1/2点,@3x设备只能使用整点、1/3或2/3点。

忽略是安全的,但如果您想摆脱错误/警告消息,请为您的单元格子视图提供 less-than-required 优先级的底部约束。

例如(根据您发布的代码,我假设 carImageView, carImage, stringStack, editButton 都是 carView 的子视图):

    let c  = carView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor)
    c.priority = .required - 1
    
    NSLayoutConstraint.activate([
        
        // don't use this one
        //carView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
        
        // activate the less-than-required bottom constraint
        c,
        
        // the rest of your constraints....
        carView.heightAnchor.constraint(equalToConstant: 120),
        carView.topAnchor.constraint(equalTo: contentView.topAnchor),
        carView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
        carView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),

        // and so on....