ios - 带有圆角的自定义 uicollectionViewCell selectedBackgroundView

标签 ios uicollectionviewcell uibezierpath

我正在尝试为我的 collectionView 单元格创建自定义 selectedBackgroundView。我将 UIView 子类化,这是我的 drawRect 实现:

override func drawRect(rect: CGRect) {
        let context = UIGraphicsGetCurrentContext()
        CGContextSaveGState(context)
        let bezierPath = UIBezierPath(roundedRect: rect, cornerRadius: 5.0)
        bezierPath.lineWidth = 5
        let color = UIColor(red: 0, green: 0, blue: 0, alpha: 0)
        color.setStroke()

        UIColor(red:0.529, green:0.808, blue:0.922, alpha:1).setFill()
        bezierPath.fill()
        bezierPath.stroke()
        CGContextRestoreGState(context)
    }

下图描述了选中单元格时我得到的结果。

enter image description here

正如你所看到的,我得到了这个丑陋的黑角。我希望黑角完全透明。我怎样才能做到这一点?感谢您的帮助。

最佳答案

您可以将 View 层的角半径设置为 5。

self.view.layer.cornerRadius = 5;

并将 View 的 clipsToBounds 属性设置为 true。

self.view.clipsToBounds = true;

关于ios - 带有圆角的自定义 uicollectionViewCell selectedBackgroundView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31439646/

相关文章:

ios - 如果在集合的两侧,UICollectionViewCell 在缩放时不会绘制阴影

ios - UICollectionView 渲染问题

ios - MKMapPoint 到 CGPoint

ios - 使用现有路径为 UIView 创建掩码?

ios - 需要从呈现的 UINavigationController 中返回到呈现的 UIViewController

iphone - 安排并显示本地通知后,启动 iPhone 应用程序需要什么代码?

ios - 调整 UICollectionViewCells 的大小以始终填充 UICollectionView

iphone - 如何将坐标从英寸转换为像素?

ios - 必须在 Swift 1.2 中调用父类(super class) 'UIViewController' 的指定初始化程序

ios - 如何在简单的图形编辑器中实现橡皮擦?