custom method in custom view for subview layout, or use of layoutSubviews/layoutIfNeeded
I have a custom UIView that is used within a UIViewController. The custom view needs some custom/programtic layout. What I'm curr开发者_StackOverflow中文版ently 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?
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.
精彩评论