UITableViewCell: rounded corners and shadow

前端 未结 14 1374
故里飘歌
故里飘歌 2020-12-02 05:05

I\'m changing the width of a UITableViewCell so that the cell is smaller but the user can still scroll along the edges of the tableview.

override func layout         


        
14条回答
  •  無奈伤痛
    2020-12-02 05:14

    To create shadow and corner for cell you need only one backView. See my example below.

    You have to add backView and set leading, trailing, top, bottom constraints equal to Content view. Put you content to backView with appropriate constraints, but be sure your content not over cover backView.

    After that in your cell initialisation code add these lines:

    override func awakeFromNib() {
        super.awakeFromNib()
    
        backgroundColor = Colors.colorClear
    
        self.backView.layer.borderWidth = 1
        self.backView.layer.cornerRadius = 3
        self.backView.layer.borderColor = Colors.colorClear.cgColor
        self.backView.layer.masksToBounds = true
    
        self.layer.shadowOpacity = 0.18
        self.layer.shadowOffset = CGSize(width: 0, height: 2)
        self.layer.shadowRadius = 2
        self.layer.shadowColor = Colors.colorBlack.cgColor
        self.layer.masksToBounds = false
    }
    

    Don't forget to create IBOutlet for Back View.

    And here the result:

提交回复
热议问题