2

I have a custom UIView that is used within a UIViewController. The custom view needs some custom/programtic layout. What I'm currently doing is, from within the controller "viewDidAppear" method I'm calling a custom view method I've put in place called "layoutThisView". This seems to work OK.

QUESTION - Should/could I use instead of this approach the UIView layoutSubviews/layoutIfNeeded methods? would this help/be a better approach?

Greg
  • 34,042
  • 79
  • 253
  • 454

1 Answers1

4

It probably would be better to use layoutSubviews. Cocoa Touch will send this message automatically whenever the view needs layout for any reason--resizing, orientation changes, anything. In general, go with the framework wherever you can.

One caveat: I believe that scrolling a UIScrollView may cause layoutSubviews to be called on its containing view, so you may want to do something like make sure the view bounds have actually changed before performing certain types of layout in that case. I'd be happy for a commenter to clarify that, though--it's just something I've run into, not something I've tested thoroughly.

Joe Osborn
  • 1,145
  • 7
  • 10
  • so do I implement a layoutSubview method in the custom UIView, and then the parent UIControllerView will automatically know when to call this method? – Greg Aug 04 '11 at 05:53
  • Yes, exactly, though I believe it's instigated by the window and view hierarchy. – Joe Osborn Aug 04 '11 at 15:21