Skip to content

Instantly share code, notes, and snippets.

@nor0x
Created December 22, 2016 17:23
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nor0x/076cef18b1e412d2f432da911b9a5bab to your computer and use it in GitHub Desktop.
Save nor0x/076cef18b1e412d2f432da911b9a5bab to your computer and use it in GitHub Desktop.
UICollectionViewCell -> Corner Radius AND Shadow
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
cell.contentView.layer.cornerRadius = 2.0
cell.contentView.layer.borderWidth = 1.0
cell.contentView.layer.borderColor = UIColor.clear.cgColor
cell.contentView.layer.masksToBounds = true;
cell.layer.shadowColor = UIColor.lightGray.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
return cell
}
@saturngod
Copy link

Thanks. It work.

@Yerazhas
Copy link

Yerazhas commented Jan 3, 2018

it doesn't work properly

@gersonmontenegro
Copy link

I only needed to add the masksToBounds property to false, and then it worked. Thanks!

@pushprajchaudhary
Copy link

pushprajchaudhary commented Mar 7, 2018

I have a scenario in which i want corner radius with shadow (without border).
Content view is not working for me in above code so I added one view name designable view below is my code.

    cell.designableView.layer.cornerRadius = 10.0
    cell.designableView.layer.masksToBounds = true
    
    cell.layer.shadowColor = UIColor.black.cgColor
    cell.layer.shadowOffset = CGSize(width: 0, height: 0.0)
    cell.layer.shadowRadius = 10.0
    cell.layer.shadowOpacity = 1.0
    cell.layer.masksToBounds = false
    cell.layer.shadowPath = UIBezierPath(roundedRect: cell.bounds, cornerRadius: cell.contentView.layer.cornerRadius).cgPath

@maulik6994
Copy link

how Can I put shadow on all four side?

@FarisAlbalawi
Copy link

Thank you

@Akash2002
Copy link

how Can I put shadow on all four side?

You set the width and height to some values. CGSize(width: #, height: #) instead of 0

Copy link

ghost commented Mar 27, 2019

Thanks, really helped.

@Mukeshawal
Copy link

Thanks, this saved me a lot of time.

@DhavalDobariya86
Copy link

For CollectionViewCell with dynamic width, this is not working. Since, cell.bounds is returning same value each time.

@GouthamNandanTeegala
Copy link

Thanks. its working fine.

@Kirill12a
Copy link

Thanks. its working fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment