我創建了以下 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()
}
...
}
但是,單元格以默認背景呈現。但是,如果我嘗試使用任何其他顏色:
contentView.backgroundColor = .red
這確實有效。只是用 .clear 表示它不起作用。
在使單元出隊時,我還嘗試了以下操作:
cell.backgroundView?.backgroundColor = .clear
但這也不起作用。
如何使單元格具有透明背景?
uj5u.com熱心網友回復:
正如評論所說 self.backgroundColor = UIColor.clear,足以init為TableviewCell單元格提供背景顏色。
如果你的主色在Viewcontroller's view制作中
tableview.backgroundColor = .clear // in view
and
self.backgroundColor = UIColor.clear // in cell init class
如果您的主要顏色在 tableview 的背景中,只需制作
self.backgroundColor = UIColor.clear // in cell init class
我沒有看到setConstraints方法內部,但我認為標簽覆寫了整個單元格,如果標簽有背景顏色,這可能會導致無法獲得您想要的結果。也不要忽略它
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/515083.html
標籤:IOS迅速合适的视图
