Search code examples
iosuiviewsubview

Performance of UIView: removeFromSuperview VS hide


This question is really basic. What is the performance difference between removing a UIView from the view hierarchy and hiding a UIView?

I have read that Views that are not needed should be removed from the view hierarchy. I currently have the situation that a UIButton should sometimes be visible. When do I hide the UIButton and when do I remove it from it's superview?

Is it expensive to change the view hierarchy?


Solution

  • If you need to alternate between showing and hiding the subview, the best approach is definitely hiding it. For a UIButton the memory implications are not that great anyway. And the code is certainly simpler if you just switch the hidden property.

    Also, you get the additional advantage that the hidden property is animatable!