ios – Swift: Can’t set the background of custom UITableViewCell to .clear

0
52


I have created the following UITableViewCell

class SeasonTableViewCell: UITableViewCell {

static let identifier = "SeasonTableViewCell"

private var seasonLabel: UILabel = {
    let label = UILabel()
    label.numberOfLines = 1
    label.textAlignment = .center
    return label
}()

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    contentView.backgroundColor = .clear
    [seasonLabel].forEach { contentView.addSubview($0) }
    setConstraints()
}

...

}

However, the cells render with the default background. If however I try to use any other color:

contentView.backgroundColor = .red

This does work. It’s just with .clear that it doesn’t work.

I have also tried the following when dequeueing the cell:

cell.backgroundView?.backgroundColor = .clear

But this doesn’t work either.

How can I make the cell have a transparent background?