2016-09-13 65 views
0

我想設置陰影與UICollectionViewCell,像這樣:UICollectionViewCell不能設置陰影

img

我寫的代碼在自定單元

override func awakeFromNib() { 
     super.awakeFromNib() 
     layer.shadowColor = UIColor(red: 0.7176470757, green: 0.7176470757, blue: 0.7176470757, alpha: 1.0000000000).CGColor 
     layer.shadowOffset = CGSizeMake(0, 4) 
     layer.shadowRadius = 2 
     layer.shadowOpacity = 1 
    } 

,但不能設置單元格的陰影。所有子視圖通過設置陰影:

mycell

我怎樣才能解決這個問題?

回答

0

你應該把clipsToBounds.That是問題所在。

self.layer.shadowColor = UIColor(red: 0.7176470757, green: 0.7176470757, blue: 0.7176470757, alpha: 1.0000000000).CGColor 
self.layer.shadowOffset = CGSizeMake(0, 4) 
self.layer.shadowOpacity = 1.0 
self.layer.shadowRadius = 2.0 
self.clipsToBounds = false 
self.layer.masksToBounds = false 
0

添加此行太:

layer.masksToBounds = false 

缺省情況下它是true,並限制其幀大小內的細胞,通過將其設置到false允許細胞的影子是其框架的外側是可見的。

+0

謝謝,我加了這行代碼,但仍然一樣,我檢查了document.masksToBounds默認爲NO – Neal

1

轉到collectionviewcell.m並手動添加。

我這樣做來解決它。

- (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; 
} 

它會添加效果,如果你想添加一些ui,然後在其中的代碼。

相關問題