Adding rounded corner and drop shadow to UICollectionViewCell

前端 未结 15 2637
逝去的感伤
逝去的感伤 2020-12-12 09:50

So I already went through various posts on adding 2nd view for adding shadow, but I still cannot get it to work if I want to add it in UICollectionViewCell. I s

相关标签:
15条回答
  • 2020-12-12 10:50

    You can set shadow color, radius and offset in UICollectionViewDataSource method while creating a UICollectionViewCell

    cell.layer.shadowColor = UIColor.gray.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 2.0)
    cell.layer.shadowRadius = 1.0
    cell.layer.shadowOpacity = 0.5
    cell.layer.masksToBounds = false
    
    0 讨论(0)
  • 2020-12-12 10:51

    Here the Swift 4 solution, updated to round every corners and not only the top corners:

    contentView.layer.cornerRadius = 6.0
    contentView.layer.borderWidth = 1.0
    contentView.layer.borderColor = UIColor.clear.cgColor
    contentView.layer.masksToBounds = true
    
    layer.shadowColor = UIColor.lightGray.cgColor
    layer.shadowOffset = CGSize(width: 0, height: 2.0)
    layer.shadowRadius = 6.0
    layer.shadowOpacity = 1.0
    layer.masksToBounds = false
    layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath
    layer.backgroundColor = UIColor.clear.cgColor
    
    0 讨论(0)
  • 2020-12-12 10:52

    SWIFT 4.2

    One should add this in your custom cell or cellForItemAt: If you are using the cellForItemAt: approach substitute self -> cell

            self.layer.cornerRadius = 10
            self.layer.borderWidth = 1.0
            self.layer.borderColor = UIColor.lightGray.cgColor
    
            self.layer.backgroundColor = UIColor.white.cgColor
            self.layer.shadowColor = UIColor.gray.cgColor
            self.layer.shadowOffset = CGSize(width: 2.0, height: 4.0)
            self.layer.shadowRadius = 2.0
            self.layer.shadowOpacity = 1.0
            self.layer.masksToBounds = false
    

    This will give you a cell with a rounded border and a drop shadow.

    0 讨论(0)
提交回复
热议问题