Hello, I am moving from storyboards(I actually liked them) to the code based UI. So far it was great. The problem is that I can’t solve this issue. We have a custom view, which was previously included to storyboard and we only needed to call
view.setNeedsDisplay() and it worked… Right now it does not work with code, here is customView:
class View: UIView {
var ratio :CGFloat = 125/86
override func draw(_ rect: CGRect) {
self.layer.sublayers?.removeAll()
UIRectFill(rect)
let ctx: CGContext = UIGraphicsGetCurrentContext()! //why it is null?
ctx.setBlendMode(CGBlendMode.destinationOut)
let shapeLayer = CAShapeLayer()
shapeLayer.path = contourPath.cgPath
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.strokeColor = strokeColor.cgColor
shapeLayer.lineWidth = 1.5
self.layer.addSublayer(shapeLayer)
}
}
I create it like this:
var rectangleView: View = {
let view = View(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
And then add it like this:
view.addSubview(view)
view.setNeedsDisplay()
I tried in both viewDidLayoutSubviews and viewDidLoad, but it still did not appeared!If called manually view.draw and pass any CGRect I get that UIGraphicsGetCurrentContext() is null and application crashes… This so frustrating and I don’t know how should I change my customView or what should I do?