ClipsToBounds在UITableViewCell中
问题描述:
我一直试图设置clipsToBounds
在UITableViewCell
到true
一个子类,但不知何故,再次成为false
:ClipsToBounds在UITableViewCell中
class CustomCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
contentView.clipsToBounds = true
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
print(contentView.clipsToBounds) // false
super.layoutSubviews()
print(contentView.clipsToBounds) // false
}
}
有什么理由为它在contentView
再次成为false
?我可以通过在layoutSubviews()
中设置clipsToBounds
到true
来“解决”这个问题,但它感觉不对。
答
我认为contentView
有时会稍后更改后,您设置 cell.contentView
。改为尝试cell.clipsToBounds = true
。
答
您可以尝试在layoutSubviews()
而设置的属性。 您还可以使用awakeFromNib()
函数将clipsToBounds
设置为true
..
您能解释一下您的答案吗? –
我正在以编程方式执行所有操作,所以我认为这不会起作用。并将它设置为true后,它不会因为'fatalError'而崩溃吗? –