swift - 如何只填充自定义 UIView 矩形的一部分?

标签 swift xcode uibezierpath rounded-corners

使用 UIBezierPath,我试图在自定义 UIView 的灰色背景“之上”创建一个黄色圆角矩形。如果您能帮助我理解为什么此代码片段不起作用,我将不胜感激:

class CardView: UIView {
    override func draw(_ rect: CGRect) {
        let roundedRect = UIBezierPath(roundedRect: bounds, cornerRadius: 16.0)
        UIColor.yellow.setFill()
        roundedRect.fill()
    }
}

This links to a picture of my storyboard.

感谢您的帮助!

最佳答案

我想通了。 . .我没有调用 addClip() 方法。

class CardView: UIView {

    override func draw(_ rect: CGRect) {
        let roundedRect = UIBezierPath(roundedRect: bounds, cornerRadius: 16.0)
        roundedRect.addClip()
        UIColor.yellow.setFill()
        roundedRect.fill()
    }

现在可以了。

关于swift - 如何只填充自定义 UIView 矩形的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56688251/

相关文章:

xcode - 无法在设备上运行 Swift iOS 应用程序

iphone - iOS 6 中 View Controller 的错误旋转

ios - 如何将图像拉伸(stretch)成自定义形状(swift3)

ios - 如何计算或技巧 UIBezierPath 控制点平滑形状

swift - 为什么绘制的UIBezierPath 位于右下方,而没有指定CAShapeLayer 的中心位置?

ios - 如何在 Swift 中用按钮类型覆盖 UIButton 的初始化?

ios - 如何在 SpriteKit 中播放和暂停音乐

iOS 应用剪辑 : How Can I create profile for it?

ios - 降低 UIImage 的质量(压缩它)

ios - 未调用 didTapMyLocationButtonForMapView