简体   繁体   中英

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作为另一个UIScrollView的内容

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 UIScrollView s.

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM