I am working on a comment section for my app and want my cells to resize themselves automatically. If I replace UITableViewAutomaticDimension
with an arbitrary value like 120, it looks more or less like I want it to.
However, if I leave it at UITableViewAutomaticDimension
the cells returned are literally tiny. I will add a picture at the end showing what both ways look like (left: UITableViewAutomaticDimension, right: rowHeight = 120). How can I fix this? I haven't found anyone with a similar problem since I do set the constraints, which were in many cases the cause of auto-resize problems (the subviews do have translatesAutoresizingMaskIntoConstraints
set to false
).
I am going to provide all the code which may be of interest. It's basically nothing else but a standard table view with cells which are supposed to auto-resize themselves and use constraints.
I would really appreciate your help!
CommentCell
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
func setupViews() {
//all of these subviews have .translatesAutoresizingMaskIntoConstraints = false
contentView.addSubview(profilePictureView)
contentView.addSubview(usernameLabel)
contentView.addSubview(commentLabel)
let marginGuide = contentView.layoutMarginsGuide
let viewWidth = UIScreen.main.bounds.width
NSLayoutConstraint.activate([
profilePictureView.heightAnchor.constraint(equalToConstant: 42),
profilePictureView.widthAnchor.constraint(equalToConstant: 42),
profilePictureView.leftAnchor.constraint(equalTo: marginGuide.leftAnchor),
profilePictureView.topAnchor.constraint(equalTo: marginGuide.topAnchor, constant: -2),
usernameLabel.leftAnchor.constraint(equalTo: profilePictureView.rightAnchor, constant: 16),
usernameLabel.widthAnchor.constraint(equalToConstant: viewWidth - 66),
usernameLabel.centerYAnchor.constraint(equalTo: profilePictureView.centerYAnchor, constant: -8),
commentLabel.leftAnchor.constraint(equalTo: profilePictureView.rightAnchor, constant: 16),
commentLabel.widthAnchor.constraint(equalTo: usernameLabel.widthAnchor),
commentLabel.topAnchor.constraint(equalTo: usernameLabel.bottomAnchor, constant: 4)
])
}
UITableViewAutomaticDimension
| 120
: