UIScrollView won't scroll
I'm having a problem getting UIScrollView to scroll when it is a subview of the main (root view) rather than being the root view itself.
This is a Navigation based application, so my root view controller class is called RootViewController.
Before using UIScrollView as a subview, I started with a simple test which worked fine. In this test, my RootViewController called loadView which did the following:
CGRect viewFrame=CGRextMake(0.0,0.0,320,460);
UIScrollView *theview=[[UIScrollView alloc] initWithFrame:viewFrame];
[theview setContentSize:CGSizeMake(320.0,2300.0)];
//
// CREATE SOME UIImageViews here, a 4x4 grid of some 60x60 images
// add these as subviews of theview (the UIScrollView)
// code not shown for brevity
//
self.view=theview;
[theview release];
Notice that in this simple test the actual root view is indeed a UIScrollView. So that worked fine - I could build and run the app and my 4x4 grid of images in the UIScrollView seemed to work (scroll) fine. No problem.
But what I really wanted to do was to have a background image for the entire screen, and have a UIScrollView start about 56 pixels down from the top of the screen, with a height of about 300 pixels. I wanted the UIScrollView to scroll but the background image stays in the same place on the screen. So, I decided to change the code so that the RootViewController main view is now a UIImageView, and that UIImageView contains a UIScrollView as a subview. Specifically, I modified loadView as follows:
// create the root view
CGRect viewFrame=CGRextMake(0.0,0.0,320,460);
UIImageView *theview=[[UIImageView alloc] initWithFrame:viewFrame];
theview.image=[UIImage imageNamed:@"background.png"];
// create the scroll view
CGRect scrollFrame=CGRectMake(0.0,56.0,320.0,300.0);
UIScrollView *scrollview=[[UIScrollView alloc] initWithFrame:scrollFrame];
[scrollview setContentSize:CGSizeMake(320.0,2300.0);
//
// CREATE SOME UIImageViews here, a 4x4 grid of some 60x60 images
// add these as subviews of scrollview (the UIScrollView)
// code not shown for brevity
//
// add scroll view as subview of root view
[theview addSubview:scrollview]
// save root view in controller
self.view=theview;
// release (do I also need to release scrollview here?)
[scrollview release]
[theview release];
But now the scrolling doesn't work - the content (the 4x4 grid of images in the UIScrollView) will not scroll.
So what am I doing wrong?
Thanks
|