I had the same problem. In my case, I was iterating over a UIScrollView, trying to dynamically calculate its contentSize. Making my way through subviews, I found two mysterious imageViews among subviews (not added by me) that together added exactly 100 pixels. It turns out that these two images are actually scroll indicators that are automatically added to the scroll list.
My solution was to create a container view class, call it a ContainerView, and in it I created a scroll.
Inside this ContainerView, I override the addSubview and willRemoveSubview methods, for example:
- (void)addSubview:(UIView *)view { [_addedSubviews addObject:view]; [_scrollView addSubview:view]; } - (void)willRemoveSubview:(UIView *)subview { [_addedSubviews removeObject:subview]; [_scrollView willRemoveSubview:subview]; }
Now you can add a method to calculate the size. This time we will move on to an array called _addedSubviews, not subviews scrollviews, because we know that the views inside _addedSubviews are just the ones that we added. And because of this, we will avoid the loop using the scroll indicators and something else :-)
And finally, just cycle through and figure out.
- (CGSize)desiredContentSize { for(UIView *mySubviews in _addedSubviews) {
source share