2016-09-20 62 views
1

我創建了一個collectionView,我想陰影添加到每一個細胞,我寫了這個代碼陰影不會出現在collectionViewcell

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! MyCollectionViewCell 
     cell.myLabel.text = self.items[indexPath.item] 
     cell.backgroundColor = UIColor.yellow 
     cell.layer.shadowColor = UIColor.black.cgColor 
     cell.layer.shadowOffset = CGSize(width: 3, height: 3) 
     cell.layer.shadowOpacity = 0.7 
     cell.layer.shadowRadius = 4.0 
     return cell 
    } 

但這是輸出

enter image description here

我想它是這樣的 UIView with rounded corners and drop shadow?

+1

如果你說'cell.layer.masksToBounds = false',會有幫助嗎? – matt

+0

@matt它的工作!謝謝 – amjad

+0

這很好,我會給它作爲答案。 – matt

回答

3

如果你還說:你的代碼可能會工作:

cell.layer.masksToBounds = false 

原因是陰影在圖層的邊界之外,所以如果我們遮住邊界,就不會看到陰影。

+0

稍微簡單一點:'cell.clipsToBounds = true' –

+2

@ leslie-godwin和錯誤 – matt

+0

爲什麼它錯了? 'cell.clipsToBounds'不是'cell.layer.masksToBounds'嗎? –