Adding rounded corner and drop shadow to UICollectionViewCell

前端 未结 15 2638
逝去的感伤
逝去的感伤 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:38

    I had to make some slight changes for Swift:

    cell.contentView.layer.cornerRadius = 2.0;
    cell.contentView.layer.borderWidth = 1.0;
    cell.contentView.layer.borderColor = UIColor.clearColor().CGColor;
    cell.contentView.layer.masksToBounds = true;
    
    cell.layer.shadowColor = UIColor.grayColor().CGColor;
    cell.layer.shadowOffset = CGSizeMake(0, 2.0);
    cell.layer.shadowRadius = 2.0;
    cell.layer.shadowOpacity = 1.0;
    cell.layer.masksToBounds = false;
    cell.layer.shadowPath = UIBezierPath(roundedRect:cell.bounds, cornerRadius:cell.contentView.layer.cornerRadius).CGPath;
    
    0 讨论(0)
  • 2020-12-12 10:40

    I use the following method to accomplish this kind of effect:

    extension UICollectionViewCell {
        /// Call this method from `prepareForReuse`, because the cell needs to be already rendered (and have a size) in order for this to work
        func shadowDecorate(radius: CGFloat = 8,
                            shadowColor: UIColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.3),
                            shadowOffset: CGSize = CGSize(width: 0, height: 1.0),
                            shadowRadius: CGFloat = 3,
                            shadowOpacity: Float = 1) {
            contentView.layer.cornerRadius = radius
            contentView.layer.borderWidth = 1
            contentView.layer.borderColor = UIColor.clear.cgColor
            contentView.layer.masksToBounds = true
    
            layer.shadowColor = shadowColor.cgColor
            layer.shadowOffset = shadowOffset
            layer.shadowRadius = shadowRadius
            layer.shadowOpacity = shadowOpacity
            layer.masksToBounds = false
            layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: radius).cgPath
            layer.cornerRadius = radius
        }
    }
    
    0 讨论(0)
  • 2020-12-12 10:42

    Swift 3 version:

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

    In case it helps: Here is the swift for rounding the corners:

    cell.layer.cornerRadius = 10
    cell.layer.masksToBounds = true
    

    with cell being a variable controlling the cell: often, you will use this in override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

    Enjoy!

    0 讨论(0)
  • 2020-12-12 10:44

    This one worked for me

    cell.contentView.layer.cornerRadius = 5.0
    cell.contentView.layer.borderColor = UIColor.gray.withAlphaComponent(0.5).cgColor
    cell.contentView.layer.borderWidth = 0.5
    
    let border = CALayer()
    let width = CGFloat(2.0)
    border.borderColor = UIColor.darkGray.cgColor
    border.frame = CGRect(x: 0, y: cell.contentView.frame.size.height - width, width:  cell.contentView.frame.size.width, height: cell.contentView.frame.size.height)
    
    border.borderWidth = width
    cell.contentView.layer.addSublayer(border)
    cell.contentView.layer.masksToBounds = true
    cell.contentView.clipsToBounds = true
    
    0 讨论(0)
  • 2020-12-12 10:44

    Here's my take on the solution. It's similar to other answers, with one key difference. It doesn't create a path that's dependent on the bounds of the view. Anytime you create a path based on the bounds and provide it to the layer you can run into issues when it's resizing, and need to setup methods to update the path.

    A simpler solution is to avoid using anything that is bounds dependent.

    let radius: CGFloat = 10
    
    self.contentView.layer.cornerRadius = radius
    // Always mask the inside view
    self.contentView.layer.masksToBounds = true
    
    self.layer.shadowColor = UIColor.black.cgColor
    self.layer.shadowOffset = CGSize(width: 0, height: 1.0)
    self.layer.shadowRadius = 3.0
    self.layer.shadowOpacity = 0.5
    // Never mask the shadow as it falls outside the view
    self.layer.masksToBounds = false
    
    // Matching the contentView radius here will keep the shadow
    // in sync with the contentView's rounded shape
    self.layer.cornerRadius = radius
    

    Now when ever the cells size change the view API will do all the work internally.

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