If I have a custom subclass of UIView that implements drawRect and controller methods use addSubview to create a view hierarchy in this custom view, how does drawRect interact with these subviews? Does it recreate the entire subclass’s view hierarchy from scratch and remove any existing subviews? Or does it ignore subviews and only redraw a particular view/subview?
Would it be acceptable to programmatically add and remove subviews within drawRect?
drawRectis meant to be only for drawing your content in the view.Whether it draws the entire view or part of it: It depends on your implementation. If you want to do any optimization is a good idea to check when your view calls
drawRectand adjust the code accordingly (maybe you want to update only one part of the view, maybe you don’t want to draw all the times, etc). It depends on your needsI don’t think is a good idea to add/remove subviews within
drawRectbecause this method will be called in several situations and I dare to say that is NOT what you want 🙂Instead, you could try something like this: