开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜