How to work properly with UIView and drawRect () to customize it in playground?

Why doesn't drawRect () function work in playground when I want to override it and show the view? I am trying to do this:

class CustomView: UIView {
    override func drawRect(rect: CGRect) {
        //some code to change self properties
    }
}
let rect = CGRect(x: 0.0, y: 0.0, width: 300.0, height: 300.0)
let view = CustomView(frame: rect)
view

      

Then I try to change some properties (ex: backgroundColor). But they don't apply.

+3


source to share


1 answer


I found the following case:

To apply set backgroundColor in drawRect I need to call this:

view.setNeedsDisplay()

      



BUT: From the documentation:

For example, you don't need to override this method if your view is just displaying the background color, or if your view directly sets its content using the underlying object.

+4


source







All Articles