Mazzaroth
Mazzaroth

Reputation: 801

UIScrollView as subview of another UIScrollView

Is it possible to have a small vertical scrollview as subview of another bigger vertical scrollview?

I made a small prototype. When the scrollviews are not hierarchical (if I put them side by side for example), both scroll correctly. But if I put one as a subview of the other, then only the sub scrollview scrolls when I pan it and the top scrollview seems to be locked when I pan it.

I envision that if the user pans the embedded scrollview (subSV in diagram below), then only the embedded scrollview would scroll. Similarly, if the user pans the top scrollview, then only the top scrollview would move and the embedded scrollview may be scrolled out of the visible content.

Do you know of any sample code that does this?

UIScrollView as content of another UIScrollView

Upvotes: 0

Views: 1973

Answers (2)

Mazzaroth
Mazzaroth

Reputation: 801

I was able to make it work with programmatically but not using StoryBoard. Here is the trivial piece of code:

UIScrollView *topSV = [[UIScrollView alloc] initWithFrame:CGRectMake(100.0f, 100.0f, 600.0f, 600.0f)];
topSV.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
topSV.contentSize = CGSizeMake(2000.0f, 2000.0f);

UIScrollView *subSV = [[UIScrollView alloc] initWithFrame:CGRectMake(10.0f, 10.0f, 200.0f, 200.0f)];
subSV.backgroundColor = [UIColor whiteColor];
subSV.contentSize = CGSizeMake(2000.0f, 2000.0f);

[topSV addSubview:subSV];

[self.window addSubview:topSV];

Upvotes: 0

pgb
pgb

Reputation: 25001

Apparently is fully supported from 3.0 onwards, and should be automatic (see http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/UIScrollView_pg/NestedScrollViews/NestedScrollViews.html). They provide sample code as well, which might contain an example of nested UIScrollViews.

Upvotes: 1

Related Questions