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のmasksToBounds
をNO
にする。