iPhone UIScrollView ContentSize has extra height when rotating/converting to landscape
Doing s开发者_StackOverflow社区ome iPhone dev, have the app working quite well. However, I have a UIScrollView with autoresizing content and contentSize which works quite well, except when I rotate to the landscape view it always has an extra 100 or so pixels on the scroll height. I don' want users to be able to scroll so far past the content.
Has anyone else had this issue or know of a fix?
thanks in advance,
Michael
I experienced the same problem. In my case, I was looping through a UIScrollView in an attempt to dynamically calculate it's contentSize. While looping through the subviews, I found two mysterious imageViews amongst the subviews (not added by me), which together added up to exactly 100 pixels. It turns out that these two images are actually the scroll indicator which is automatically added to the scrollview.
My solution was to create a container view class, let's call it ContainerView, and in it I created the scrollview.
Inside of this ContainerView I override the methods addSubview and willRemoveSubview, like this:
- (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 for calculating the size. This time we'll loop through the array called _addedSubviews, instead of the scrollviews subviews, because we know that the views inside _addedSubviews are only the ones added by us. And because of this we'll avoid looping through scroll indicators and whatnot :-)
And lastly just loop through and calculate.
- (CGSize)desiredContentSize {
for(UIView *mySubviews in _addedSubviews) {
// Calculate size
}
// Return size
}
Zath has an interesting solution. I've ran into similar.
I'd want to say you're not updating your contentSize after the switch to landscape, but the contentSize should be the same regardless.
Ironically I was having the opposite issue just now. However, after learning superviews must have autoresizesSubviews set to yes, and ensuring all 4 superviews have the same resizing property, it finally keeps the "buffer" space.
Lots of run after tweak and eventually you'll get what you want, unless you want the top y point to stay in the same spot when it's greater than 0.
精彩评论