Rohan Gupta
Rohan Gupta

Reputation: 21

How can I fill only part of the custom UIView rectangle?

Using UIBezierPath, I am attempting to create a yellow rounded rectangle "on top of" the gray background of the custom UIView. I'd appreciate if you could help me understand why this code snippet is not working:

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.

Thank you for your help!

Upvotes: 0

Views: 149

Answers (1)

Rohan Gupta
Rohan Gupta

Reputation: 21

I figured it out . . . I did not call the addClip() method.

class CardView: UIView {

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

}

This now works.

Upvotes: 2

Related Questions