10

In the new UICollectionView I do not see how to add a shadow to a UICollectionViewCell. How would I go about this. Would I add another view?

    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowPath = [UIBezierPath bezierPathWithRect:rect].CGPath;
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowColor = [UIColor yellowColor].CGColor;
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowRadius = .5;
    [self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.shadowOpacity = .1;

enter image description here

BDGapps
  • 3,318
  • 10
  • 56
  • 75
  • 2
    Isn't it very inefficient to call `[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]]` multiple times? – Koen. Oct 19 '14 at 15:21

5 Answers5

42

You're forgetting to set masksToBounds on UIView to NO. This should work:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PhotoCell" forIndexPath:indexPath];

    cell.layer.masksToBounds = NO;
    cell.layer.borderColor = [UIColor whiteColor].CGColor;
    cell.layer.borderWidth = 7.0f;
    cell.layer.contentsScale = [UIScreen mainScreen].scale;
    cell.layer.shadowOpacity = 0.75f;
    cell.layer.shadowRadius = 5.0f;
    cell.layer.shadowOffset = CGSizeZero;
    cell.layer.shadowPath = [UIBezierPath bezierPathWithRect:cell.bounds].CGPath;
    cell.layer.shouldRasterize = YES;

    return cell;
}
shim
  • 9,289
  • 12
  • 69
  • 108
marchinram
  • 5,698
  • 5
  • 47
  • 59
  • Won't this slow it down? Any idea if we can do it in Cell Subclass ? – CalZone Jun 17 '14 at 20:49
  • 1
    After testing this, I realised that in some cases the image gets rasterized with the wrong resolution, despite the `contentScale` being set. Setting `cell.layer.rasterizationScale = [UIScreen mainScreen].scale;` fixes this – DaGaMs Jan 25 '15 at 19:49
2
[self.collectionView cellForItemAtIndexPath:[self.collectionView indexPathForItemAtPoint:[recognizer locationInView:[self view]]]].layer.masksToBounds = NO;
Shmidt
  • 16,436
  • 18
  • 88
  • 136
1

most likely your problem is best solved with the existing answer to How do I draw a shadow under a UIView?

to be specific to your circumstance, you would probably have code that would do what the following code does (depending upon where you get your collectionView and someIndexPath in order to point to the cell you're interested in):

    UICollectionViewCell* collectionViewCell
      = [collectionView dequeueReusableCellWithReuseIdentifier:DEFINED_IDENTIFIER forIndexPath:someIndexPath];
    collectionViewCell.layer.shadowPath = [UIBezierPath bezierPathWithRect:collectionViewCell.bounds].CGPath;

there are obviously other ways to get the cell. the important thing is the 2nd line, to set the shadowPath.

Community
  • 1
  • 1
john.k.doe
  • 7,533
  • 2
  • 37
  • 64
  • This is not creating a shadow around the elements its affecting the entire cell. Please see above. – BDGapps Oct 11 '12 at 11:31
  • hmmm … well your original question said "*In the new UICollectionView I do not see how to add a shadow to a UICollectionViewCell.*". if you want to place a shadow around the elements in the cell, you'll have to collect the subviews of the UICollectionViewCell and then perform the setting of the shadow on each one individually. – john.k.doe Oct 12 '12 at 00:20
0

You are not setting the shadowOffset property on the layer.

myCell.layer.shadowOffset = CGSizeMake(10,10);
shim
  • 9,289
  • 12
  • 69
  • 108
JaroCep
  • 21
  • 1
  • It still is not creating the shadow on the white view it sets it on the entire cell. It is not a true shadow just a transformed background. – BDGapps Oct 11 '12 at 22:50
0

Go to the CustomCollectionviewCell.m and try to add this:

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //////// make shadow  of total view
        self.clipsToBounds = NO;
        self.layer.masksToBounds = NO;
        self.layer.shadowRadius = 5;
        self.layer.shadowOpacity = 0.5;
        self.layer.shadowColor = [UIColor blackColor].CGColor;
        self.layer.shadowOffset = CGSizeMake(0, 1);
        self.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.bounds].CGPath;

        // make radius of the cell
        self.layer.cornerRadius = 5;

    }
    return self;
}
Raj Aryan
  • 363
  • 2
  • 15