0

Out of the following, which is the best way to hide a view?

  • Setting frame to CGRectZero
  • Hidden Property to YES
  • alpha Property to 0.0

Does one have benefits that the other does not? In terms of saving resources at runtime? Specifically in a UITableView with reusable cells where some subviews may not be needed by one cell versus another.

Sebastian
  • 7,670
  • 5
  • 38
  • 50
Piotr Tomasik
  • 9,074
  • 4
  • 44
  • 57

2 Answers2

2

It depends on what you want/need to accomplish. For views that will likely be reused alot, a combination of view.hidden=TRUE (my choice for this case) and/or view.alpha=0.0, and maybe manipulate the z-index to make sure your view is no longer at the front.

I am not sure about the benefit of setting the view's frame to CGRectZero, it might be more appropriate to remove the entirely at that point.

In short, if you don't need it, don't keep it.

UPDATE:

With the mention table cells, also consider the possibility of creating multiple custom cells, especially if removing subviews causes layout problems. Each cell can be created based on whatever criteria you have set.

UPDATE 2:

Based on a comment left below, if the cells are complex, drawing the cell via code may be the right solution.

Mike D
  • 4,938
  • 6
  • 43
  • 99
  • Actually, although I like using custom cells in IB, it's a bad idea if you have a performance bottleneck. Complex cells are best done with no subviews at all, via `drawRect`. This question has more details: http://stackoverflow.com/questions/1352479/tricks-for-improving-iphone-uitableview-scrolling-performance – lxt Mar 03 '13 at 23:35
  • @lxt Again, it all depends on one's needs. – Mike D Mar 03 '13 at 23:37
0

With UIView's, alpha=0 has the advantage of being animatable whereas hidden=YES does not. frame=CGRectZero is also animatable but is quite a different effect. When animating alpha=0.0 will cause the view to fade out whereas frame=CGRectZero will cause it to appear to vanish into the upper left. I wouldn't worry about the system resource expense of any of these techniques as they're all pretty lightweight.

Mike D
  • 4,938
  • 6
  • 43
  • 99
Rich Schonthal
  • 472
  • 2
  • 12
  • if you need to animate and fade out you can indeed go to alpha 0, but then in the completion block i would set hidden=YES as well, to avoid drawing a non visible view – Ultrakorne Mar 03 '13 at 23:30