Search code examples
swiftuiviewdrawrectcgcontextibdesignable

invalid context 0x0 within custom UIView


I have the following code for a custom view.

@IBDesignable class SplitCircleView: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        draw(frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        draw(frame)
    }

    override func draw(_ rect: CGRect) {

        self.backgroundColor = .clear

        drawSlice(rect: self.frame, startPercent: 87.5, endPercent: 37.5, color: .green)
        drawSlice(rect: self.frame, startPercent: 37.5, endPercent: 87.5, color: .red)
    }

    func drawSlice(rect: CGRect, startPercent: CGFloat, endPercent: CGFloat, color: UIColor) {

        let center = CGPoint(x: rect.origin.x + rect.width / 2, y: rect.origin.y + rect.height / 2)
        let radius = (min(rect.width, rect.height) / 2)
        let startAngle = startPercent / 100 * .pi * 2 - .pi
        let endAngle = endPercent / 100 * .pi * 2 - .pi
        let path = UIBezierPath()

        path.move(to: center)
        path.addArc(withCenter: center, radius: radius, startAngle: startAngle, endAngle: endAngle, clockwise: true)
        path.close()
        color.setFill()

        path.fill()
    }
}

I'm trying to draw a circle with two semi circles different colors.

This looks fine when I view it in a playground live view. Once I put it an app it causes me problems. When the code tries to execute color.setFill() and path.fill() I get the following errors in the log.

2018-06-01 14:37:08.118005+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118055+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118094+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118141+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118184+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118222+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118336+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFillColorWithColor: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118376+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118413+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextSetFlatness: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118451+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextAddPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118491+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextDrawPath: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable. 2018-06-01 14:37:08.118528+0100 SplitCircleView[21353:2290599] [Unknown process name] CGContextRestoreGState: invalid context 0x0. If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.

What am I doing wrong and how can I fix this?


Solution

  • In the documentation for the draw(_ rect: CGRect) function. (Last paragraph of the "Discussion".)

    You should never call this method directly yourself.

    If you want to trigger this function you should use...

    self.setNeedsDisplay()
    

    Documentation for the draw function.

    However

    As @vacawama pointed out (and I missed) the draw function will be called after adding your view to the view hierarchy anyway. So you shouldn't need to call it at all here. It will happen automatically for you.

    So inside the init methods, just remove the call altogether.

    The place to call it again is if the view changes shape for instance or if you want to change the contents of it. i.e. colour, or something.