I have a UICollectionViewCell
subclass and I need to round its corners and add a shadow as well. The cell looks like a square card, and the cells have a good amount of space in-between them.
So "underneath" every cell, I would like to add some shadow. I can successfully do this, but then my cell only has rounded corners on the bottom. The top just has normal corners. I need rounded corners for all four corners.
I have found solutions on here for UIViews
that recommend adding a separate UIView
as a subview
, but I would prefer to avoid this for performance reasons.
I did find one solution which was to use this method which you will find in my code below:
[UIBezierPath bezierPathWithRoundedRect: cornerRadius:]
But that didn't work for me either. Is it possible that it's not working for me because of how I'm trying to only add the shadow "underneath" / at the bottom of the cell? It seems like most of these answers are provided for questions where the developer wants to add a shadow around the entire cell.
I guess I would be willing to add a special subview
to my UICollectionViewCell
subclass, but I would like to use that as a last resort.
I am targeting iOS 7+
and using Xcode 6.1.1.
Here is the code I am using inside my UICollectionViewCell
subclass to try and achieve both the shadow and the rounded corners:
- (void)load:(CustomUserObject *)customObject
{
self.customObject = customObject;
// Round cell corners
self.layer.cornerRadius = 12;
// Add shadow
self.layer.masksToBounds = NO;
self.layer.shadowOpacity = 0.75f;
self.layer.shadowRadius = 10.0f;
self.layer.shouldRasterize = NO;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(self.frame.size.width/2 - (self.frame.size.width - 50)/2, self.frame.size.height, self.frame.size.width - 50, 10) cornerRadius:self.layer.cornerRadius].CGPath;
}
EDIT: If I set self.layer.masksToBounds
to NO
, the shadow works but the top corners do not round. If I set self.layer.masksToBounds
to YES
, the shadow does not work, but all four corners are now rounded. I just can't figure out how to round all four corners and get the shadow to work.
shadowPath
instead of letting the system trace the layer? – Griggself.layer.shadowRadius
,self.layer.shadowOpacity
, andself.layer.shadowOffset
, the system will trace the edges of your layer. If you were to use an irregular mask (ie, a png with jagged transparent edges), it would trace those as well without requiring you to construct the path. – Griggself.layer
on the wrong object. If you want individual cells to have four rounded corners, make sure thatself
refers to the cell. If it refers to the collection view, then it will round the corners of the list (two top corners and two bottom corners). – GriggshadowPath
insetBounds
(after calling[super setBounds:]
) so that it gets updated anytime the bounds change. – AdorableTLCollectionViewController
, but that class does absolutely nothing to the cell other than dequeue it. I'd suggest moving more and more parts of your code into this project until it breaks and then you'll have a good idea whats wrong. – Adorable