ios - UICollectioviewCell 中的 Uiview 的左下角和右下角半径问题

标签 ios swift uikit uicollectionviewcell uibezierpath

UICollectionView Cell 出现问题。我试着用下面的代码只给 UIView 两个边角半径。我面临以下问题。任何建议都将不胜感激。谢谢

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

最佳答案

您可能想尝试以下代码来处理不同的 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)

    }
}

关于ios - UICollectioviewCell 中的 Uiview 的左下角和右下角半径问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55823727/

相关文章:

swift - 调用中参数 'dictionary' 缺少参数

iphone - 从 UITextView 中提取第一个单词

iphone - NSFetchedResultsController 中的 NSPredicate 抛出 EXC_BAD_ACCESS

ios - Swift 2.2 - SpriteKit - 子类 SKSpriteNode 类

ios - Swift:第二个动画没有运行

ios - 在 Swift 中智能搜索解析用户名不起作用

ios - iPhone X 横向的 UICollectionView

cocoa-touch - 如何将 subview 上触摸事件的坐标转换为其父 View 中的坐标?

ios - 使 iOS 操作扩展收到主机应用程序激活的通知

ios - 应用发布 iAd 横幅不显示广告