如何使 UICollectionViewCells 具有圆角和阴影?

Kev*_*566 1 uiview ios uicollectionview uicollectionviewcell swift

我已经看到了类似问题的其他一些答案,但即使进行了一些调整,这些解决方案都对我不起作用。

正如标题所示,我希望我的UICollectionViewCells 有圆角和阴影(或除了顶部之外的所有侧面都有阴影)。到目前为止,我所做的是将两个视图添加到我的UICollectionViewCell-中mainView,它显示单元格的必要内容并具有圆角,并且shadowView,它具有阴影。两个视图都不是另一个视图的子视图,它们一起存在于同一个单元格中。它们的尺寸完全相同,并且mainView明显显示在 的顶部shadowView。这是我当前的代码实现:

let mainView = cell.viewWithTag(1003) as! UIView       
mainView.layer.cornerRadius = 10.0
mainView.layer.borderWidth = 1.0
mainView.layer.borderColor = UIColor.clear.cgColor
mainView.layer.masksToBounds = true

let shadowView = cell.viewWithTag(1002) as! UIView
shadowView.layer.shadowColor = UIColor.black.cgColor
shadowView.layer.shadowOffset = CGSize(width: 0, height: 2.0)
shadowView.layer.shadowRadius = 2.0
shadowView.layer.shadowOpacity = 0.5
shadowView.layer.masksToBounds = false
shadowView.layer.shadowPath = UIBezierPath(roundedRect: shadowView.bounds, cornerRadius: mainView.layer.cornerRadius).cgPath
Run Code Online (Sandbox Code Playgroud)

以下是这段代码产生的结果: 在此输入图像描述 有人会认为mainView它的角不够圆,阴影不够大。

然而,在删除 的背景shadowView并将其设置UICollectionView为黑色后,您可以看到 的mainView角实际上非常圆滑: 在此输入图像描述

所以这意味着问题在于 的shadowView阴影大小。但是,我尝试增加阴影偏移和阴影半径,但没有任何作用。大多数变化要么在周围产生非常厚的阴影mainView,要么减少mainView要么进一步减少舍入,或者什么也不做。

UITableViewCell这是Storyboard 中 相关视图层次结构的图像:在此输入图像描述

few*_*ode 7

这是Cell代码:

class CollectionViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)


        layer.shadowColor = UIColor.lightGray.cgColor
        layer.shadowOffset = CGSize(width: 0, height: 2.0)
        layer.shadowRadius = 5.0
        layer.shadowOpacity = 1.0
        layer.masksToBounds = false
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: contentView.layer.cornerRadius).cgPath
        layer.backgroundColor = UIColor.clear.cgColor

        contentView.layer.masksToBounds = true
        layer.cornerRadius = 10
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
Run Code Online (Sandbox Code Playgroud)

这是使用代码:

import UIKit

private let reuseIdentifier = "Cell"

class CollectionViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.register(CollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        (self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout).itemSize = CGSize(width: 100, height: 100)
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 10
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
        cell.backgroundColor = .white
        return cell
    }
}
Run Code Online (Sandbox Code Playgroud)

结果如下: 在此输入图像描述