CGContext always creating a black rect.


Alex Zavatone
 

Hi.  I’m trying to draw a bezier shape in CGContext on a UIView in Swift with a transparent background and the background is always black.  Nothing online helps.

Any ideas?  I’ve checked .isOpaque, backgroundColor.   Nothing I can do to get rid of the black rect background.  Any ideas?  

Thanks in advance.


    override func draw(_ rect: CGRect)
    {
        self.backgroundColor = .clear
        self.isOpaque = false
        super.draw(rect)
        roundRect(rect)
    }

    

    internal func roundRect(_ rect: CGRect)
    {
        self.backgroundColor = UIColor.clear

        

        // Make a pill shape if rectCornerRadius < 0
        let xHalf = self.frame.width / 2
        let yHalf = self.frame.height / 2

        

        if (self.rectCornerRadius < 0) {
            if (self.rectWidth <= self.rectHeight) {
            self.rectCornerRadius = (yHalf / 2)
            } else if (self.rectHeight <= self.rectWidth) {
                self.rectCornerRadius = (xHalf / 2)
            }
        }

        

        let ctx: CGContext = UIGraphicsGetCurrentContext()!

        ctx.clear(rect)
        ctx.saveGState()

        

        ctx.setLineWidth(rectBorderWidth)
        ctx.setStrokeColor(rectBorderColor.cgColor)

        

        let rect = CGRect(x: 0, y: 0, width: self.rectWidth, height: self.rectHeight)

        let clipPath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: self.rectCornerRadius).cgPath
        let linePath: CGPath = UIBezierPath(roundedRect: rect, cornerRadius: self.rectCornerRadius).cgPath

        

        ctx.addPath(clipPath)

        

        ctx.setFillColor(self.rectBgColor.cgColor)
        ctx.closePath()
        ctx.fillPath()

        

        ctx.addPath(linePath)
        ctx.strokePath()

        

        ctx.restoreGState()
    }

Join {cocoa@apple-dev.groups.io to automatically receive all group messages.