ClipsToBounds在UITableViewCell中

问题描述:

我一直试图设置clipsToBoundsUITableViewCelltrue一个子类,但不知何故,再次成为falseClipsToBounds在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()中设置clipsToBoundstrue来“解决”这个问题,但它感觉不对。

我认为contentView有时会稍后更改后,您设置 cell.contentView。改为尝试cell.clipsToBounds = true

+0

您能解释一下您的答案吗? –

+0

我正在以编程方式执行所有操作,所以我认为这不会起作用。并将它设置为true后,它不会因为'fatalError'而崩溃吗? –

您可以尝试在layoutSubviews()而设置的属性。 您还可以使用awakeFromNib()函数将clipsToBounds设置为true ..