Question

I'm trying to copy a UIView with some properties. The UIView is overriding the drawLayer: method to draw custom shapes.

I'm using the NSKeyedArchiver, a pasteboard and NSKeyedUnarchiver to get back the properties. An instance of UIView is created and the properties are set - everything is fine till now.

Some properties get created in the drawlayer method, which have the default values, not the copied ones. My question is when initWithCoder is called, and an instance is created, isn't drawRect or drawLayer called? Why?

Was it helpful?

Solution

drawLayer/drawRect are called on demand (sparingly) when content needs to be rendered or re-rendered. It's probably not called during initialization because there's no need. It should be called later when the view goes on screen and needs to be rendered.

Also, you say you are creating some properties in drawLayer? Consider moving those out to some initialization method and keep the drawing methods focussed on drawing only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top