UICollectionViewCellに影をつける

UICollectionViewCellに影をつけるときはmasksToBoundsプロパティをNOにする必要がある。

以下のようにする。

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
                  cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //カスタムセルを取得
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell"
                                                           forIndexPath:indexPath];
    
    
    //影をつける
    cell.layer.masksToBounds = NO; //必須
    cell.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
    cell.layer.shadowOpacity = 0.9f;
    cell.layer.shadowRadius = 2.0f;
    
    return cell;
}

masksToBoundsプロパティは、セルの境界からはみ出ているものを見えなくする為のもので、このプロパティをNOにすると今まで隠れていたものが見える場合がある。

そういう場合は、はみ出ているUIViewのlayerのmasksToBoundsプロパティをYESにする。

例えば、次のように、Cell内にUIImageViewを配置して居た場合、UIImageViewの画像がはみ出すことがある。その場合は、UIImageViewのmasksToBoundsプロパティをYESにして、cellのmasksToBoundsNOにする。

20140904151753