不在viewdidload中时如何自定义标签?

问题描述

I have those variables in collectionviewcell file and I want to change label background and cornerradius but that file doesnt have a viewdidload how can I do that . Thanks for helps.

解决方法

UIView 子类没有 viewDidLoad 方法,而是使用视图的初始值设定项:

class MyCell: UICollectionViewCell {

   override init(frame: CGRect) {
       super.init(frame: frame)
       layoutUI()
   }

   required init?(coder: NSCoder) {
       super.init(coder: coder)
       layoutUI()
   }

   private func layoutUI() { 
        label.layer.cornerRadius = 5
        label.backgroundColor = .blue
   }
}