I am building a TableView filled with a variable number of cells. One type of those cells should have a fixed height of 69.0, but xcode keeps setting its encapsulated layout height .333 larger than what I set the height to, meaning it breaks the constraints because the encapsulated layout height is 69.333. If I change my Cell’s size to 70 for example, the constraint is set to 70.333. I don’t understand what causes this.
Here is the Cell:
class AnnotationCell: UITableViewCell {
//Set identifier to be able to call it later on
static let identifier = "AnnotationCell"
var annotation: Annotation!
//MARK: - Configure
public func configure(annotation: Annotation) {
self.annotation = annotation
}
//MARK: - Cell Style
//Add all subviews in here
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
//Customize Cell
contentView.backgroundColor = colors.darkGray
contentView.layer.borderWidth = 0
//Favorite
let favoriteView = UIView()
favoriteView.backgroundColor = colors.gray
favoriteView.addBorders(edges: [.top], color: colors.lightGray, width: 1)
favoriteView.translatesAutoresizingMaskIntoConstraints = false
let favIcon = UIImageView()
let myEmoji = "👀".textToImage() //Test
favIcon.image = myEmoji
favIcon.contentMode = .scaleAspectFill
favIcon.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(favIcon)
let stringStack = UIStackView()
stringStack.axis = .vertical
stringStack.spacing = 6
stringStack.translatesAutoresizingMaskIntoConstraints = false
let titleString = UILabel()
titleString.text = "Test"
titleString.textColor = colors.justWhite
titleString.font = UIFont(name: "montserrat", size: 17)
titleString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(titleString)
let addressString = UILabel()
addressString.text = "Test 2"
addressString.textColor = colors.lightGray
addressString.font = UIFont(name: "montserrat", size: 14)
addressString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(addressString)
favoriteView.addSubview(stringStack)
let editButton = UIButton()
editButton.tintColor = colors.lightGray
let mediumConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold, scale: .medium)
let mediumEditButton = UIImage(systemName: "chevron.right", withConfiguration: mediumConfig)
editButton.setImage(mediumEditButton, for: .normal)
editButton.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(editButton)
contentView.addSubview(favoriteView)
//Define Constraints
NSLayoutConstraint.activate([
favoriteView.heightAnchor.constraint(equalToConstant: 69),
favoriteView.topAnchor.constraint(equalTo: contentView.topAnchor),
favoriteView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
favoriteView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
favoriteView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
favIcon.leadingAnchor.constraint(equalTo: favoriteView.leadingAnchor, constant: 15),
favIcon.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
favIcon.heightAnchor.constraint(equalToConstant: 50),
favIcon.widthAnchor.constraint(equalToConstant: 50),
stringStack.leadingAnchor.constraint(equalTo: favIcon.trailingAnchor, constant: 20),
stringStack.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: favoriteView.trailingAnchor, constant: -15),
editButton.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
])
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I am not setting the Cell’s height in the TableView at all – I’ve tried that and that did not make things better. The Cell is getting displayed correctly, but console throws:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x281d21ae0 UIView:0x15cca3490.height == 69 (active)>",
"<NSLayoutConstraint:0x281d21bd0 V:|-(0)-[UIView:0x15cca3490] (active, names: ‘|’:UITableViewCellContentView:0x15cca42a0 )>",
"<NSLayoutConstraint:0x281d22120 UIView:0x15cca3490.bottom == UITableViewCellContentView:0x15cca42a0.bottom (active)>",
"<NSLayoutConstraint:0x281d28820 ‘UIView-Encapsulated-Layout-Height’ UITableViewCellContentView:0x15cca42a0.height == 69.3333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x281d21ae0 UIView:0x15cca3490.height == 69 (active)>
I have tried giving every subview a fixed height (which does not make sense with a stackview in this layout) but that did not work either. I had the same layout in a view (not TableViewCell) without any issues, so I am guessing that it has to do something with the TableView.
Here are my constraints as seen from the view hierarchy, in case someone has an eye for it. I have opened up most constraints to make it as clear as possible. As can be seen next to the red arrow, the constraint of 69.0 seems to be there and I don’t see anything different. The only thing I could see causing this issue would be the "UISysmenBackGroundView" which has a view inside it without any constraints.
>Solution :
1/3 pt sounds like a hairline thickness on a 3x device. Could it perhaps be the separator? If they’re enabled, try turning them off to see if that fixes the issue.
