I have a subclass of UICollectionViewCell and I need to round its corners and add a shadow. A cell looks like a square map, and the cells have a good space between them.
So, βbelowβ each cell, I would like to add a shadow. I can do this, but then my camera only has rounded corners at the bottom. The top just has normal angles. I need rounded corners for all four corners.
I found here solutions for UIViews that recommend adding a separate UIView as a subview , but I would prefer to avoid this for performance reasons.
I found one solution that should have used this method, which you will find in my code below:
[UIBezierPath bezierPathWithRoundedRect: cornerRadius:]
But that didn't work either. Is it possible that this does not work for me due to the way I try to add only the shadow βbottomβ / bottom of the cell? It seems that most of these answers are provided for questions on which the developer wants to add a shadow around the entire cell.
I suppose I would like to add a special subview to my subclass of UICollectionViewCell , but I would like to use it as a last resort.
I am targeting iOS 7+ and using Xcode 6.1.1.
Here is the code I'm using inside my subclass of UICollectionViewCell to try to achieve shadow and 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 are not rounded. If I set self.layer.masksToBounds to YES , the shadow does not work, but all four corners are rounded. I just can't figure out how to get around all four corners and make the shadow work.
ios calayer uicollectionviewcell cgpath
user3344977 Jan 13 '15 at 7:11 2015-01-13 19:11
source share