UICollectioviewCell中的Uiview的左下角和右下角半径

时间:2019-04-24 06:34:47

标签: ios swift uikit uicollectionviewcell uibezierpath

我遇到UICollectionView Cell的一个问题。我尝试使用下面的代码仅给UIView提供2个边角半径。我面临以下问题。任何建议,不胜感激。谢谢

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell : FeaturedCell = collectionView.dequeueReusableCell(withReuseIdentifier: "FeaturedCell", for: indexPath) as! FeaturedCell
        let objModel = arrSearch[indexPath.row]
        cell.lblproducttitle.text = "\(objModel.Name!) \n$\(objModel.price!)"
        cell.imgproduct.sd_setImage(with: URL(string: objModel.imgUrl), placeholderImage: UIImage(named: "logo"), options: .refreshCached, completed: nil)

        let mask = CAShapeLayer()
        mask.bounds = cell.productnamevw.frame
        mask.position = cell.productnamevw.center
        mask.backgroundColor = cell.productnamevw.backgroundColor?.cgColor

        let path = UIBezierPath(roundedRect: cell.productnamevw.bounds, byRoundingCorners: [.bottomLeft , .bottomRight], cornerRadii: CGSize(width: 10, height: 10))
        mask.path = path.cgPath
        cell.productnamevw.layer.mask = mask
        cell.productnamevw.layer.masksToBounds = true
        return cell

    }

Issue image

Expectation Image

3 个答案:

答案 0 :(得分:2)

您可能需要尝试以下代码来处理diff iOS版本

  1. 添加UIView扩展名,您可以根据需要对其进行修改。
extension UIView {
  func roundCorners(corners: UIRectCorner, radius: CGFloat) {
        if #available(iOS 11.0, *) {
            let cornerMasks = [
                corners.contains(.topLeft) ? CACornerMask.layerMinXMinYCorner : nil,
                corners.contains(.topRight) ? CACornerMask.layerMaxXMinYCorner : nil,
                corners.contains(.bottomLeft) ? CACornerMask.layerMinXMaxYCorner : nil,
                corners.contains(.bottomRight) ? CACornerMask.layerMaxXMaxYCorner : nil,
                corners.contains(.allCorners) ? [CACornerMask.layerMinXMinYCorner, CACornerMask.layerMaxXMinYCorner, CACornerMask.layerMinXMaxYCorner, CACornerMask.layerMaxXMaxYCorner] : nil
                ].compactMap({ $0 })

            var maskedCorners: CACornerMask = []
            cornerMasks.forEach { (mask) in maskedCorners.insert(mask) }

            self.clipsToBounds = true
            self.layer.cornerRadius = radius
            self.layer.maskedCorners = maskedCorners
        } else {
            let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
            let mask = CAShapeLayer()
            mask.path = path.cgPath
            self.layer.mask = mask
        }
    }
}
  1. 使用如下功能:
cell.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)

还有一件事,对于您的情况,我建议您在单元格本身中设置单元格

class FeaturedCell: UICollectionViewCell {

    @IBOutlet private weak var productnamevw: UIView!

    override func awakeFromNib() {
        super.awakeFromNib()

        self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)
    }

    override func layoutSubviews() {
        super.layoutSubviews()

        self.productnamevw.roundCorners(corners: [.bottomLeft, .bottomRight], radius: 10)

    }
}

答案 1 :(得分:1)

请注意,这仅适用于iOS 11.0 +

yourview.layer.maskedCorners = [.layerMaxXMaxYCorner, .layerMinXMaxYCorner, .layerMinXMinYCorner]

答案 2 :(得分:1)

对于Swift 4+和iOS 11.0之后的版本,这将起作用:

    - For top left corner = [.layerMinXMinYCorner]

    - For top right corner = [.layerMaxXMinYCorner]

    - For bottom left corner = [.layerMinXMaxYCorner]

    - For bottom right corner = [.layerMaxXMaxYCorner]

因此,您的代码应如下所示:

    cell.productnamevw.layer.cornerRadius = 10   //or whatever you want to give
    cell.productnamevw.layer.maskedCorners = [. layerMinXMaxYCorner, . layerMaxXMaxYCorner]