7
votes

I subclassed UIScrollView (IPhone SDK) and overrode the (void)layoutSubviews; method. I noticed that each time the scrollView is scrolled, this method is called.

Is that the correct behaviour or do I have mistakes in my code? If it is the default behaviour, isn't this a performance killer?

Sincerely, heinrich

1
Why are you subclassing UIScrollView? Why don't you just add subviews to it? - Martin Gordon
I could also add subviews, but thats not the point. - Erik

1 Answers

12
votes

It is the correct behaviour and it should be used to get a custom layout of your subviews. I have used it several times and haven't had any performance issues eaven with hundreds of items added.

A cut-out from the documentation on that topic:

Subclasses can also be containers for other views. In this case, just override the designated initializer, initWithFrame:, to create a view hierarchy. If you want to programmatically force the layout of subviews before drawing, send setNeedsLayout to the view. Then when layoutIfNeeded is invoked, the layoutSubviews method is invoked just before displaying. Subclasses should override layoutSubviews to perform any custom arrangement of subviews.