The code below is where I set the constraints of the UITableViewCell.
private func configureConstraints() {
let agentIconImageViewConstraints = [
agentIconImageView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 10),
agentIconImageView.topAnchor.constraint(equalTo: contentView.topAnchor,constant: 20),
agentIconImageView.widthAnchor.constraint(equalToConstant: 64),
agentIconImageView.heightAnchor.constraint(equalToConstant: 32)
]
let flightTimeAndAirportsStackViewConstraints = [
flightTimeAndAirportsStackView.leadingAnchor.constraint(equalTo: agentIconImageView.trailingAnchor, constant: 10),
flightTimeAndAirportsStackView.topAnchor.constraint(equalTo: agentIconImageView.topAnchor, constant: -5),
flightTimeAndAirportsStackView.bottomAnchor.constraint(equalTo: agentIconImageView.bottomAnchor)
]
let flightStopsAndTimeDurationStackViewConstraints = [
flightStopsAndTimeDurationStackView.topAnchor.constraint(equalTo: flightTimeAndAirportsStackView.topAnchor),
flightStopsAndTimeDurationStackView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
flightStopsAndTimeDurationStackView.bottomAnchor.constraint(equalTo: flightTimeAndAirportsStackView.bottomAnchor)
]
NSLayoutConstraint.activate(agentIconImageViewConstraints)
NSLayoutConstraint.activate(flightTimeAndAirportsStackViewConstraints)
NSLayoutConstraint.activate(flightStopsAndTimeDurationStackViewConstraints)
}
However, as can be seen in the image, it extends beyond the cell. Are there any constraints I forgot to set? otherwise why is this happening
2
Answers
It looks like you are setting constraints for several UI elements
(agentIconImageView, flightTimeAndAirportsStackView, flightStopsAndTimeDurationStackView)
within aUITableViewCell
. It appears that some of the constraints may be causing the elements to appear outside of the cell.One potential issue is the negative constant value being used on the
topAnchor
of theflightTimeAndAirportsStackView (constant: -5)
. This could be causing the stack view to be positioned above theagentIconImageView
. Additionally, thebottomAnchor
offlightStopsAndTimeDurationStackView
is constrained toflightTimeAndAirportsStackView.bottomAnchor
, this could cause both stack views to be positioned outside the cell.You might want to check and adjust the constant values, and also check if the components are going outside of the cell.
You haven’t given any constraint to the bottom of the contentview. The content view can not infer the height automatically. Either give a calculated height for the cell from your tableview delegate, or give some item a bottom constraint to contentview.bottomAnchor.