views:

204

answers:

2

I am trying to scroll the content without scrolling the image in the background using UIScrollView.

I am using IB and setting the FileOwner View to point to the Scroll View (Image View is a child of the Scroll view). I have made the image height to be 960 pixels.

I have also set scrolling content size in the vierController that owns this UIView

  • (void)viewDidLoad { UIScrollView *tempScrollView = (UIScrollView *)self.view; tempScrollView.contentSize=CGSizeMake(320, 960); }

My problem is that the Image only appears moves along with the content.

I have tried taking out the settings in viewDidLoad, but the scrolling cease to function.

I have also tried changing the location of the image and have it placed under VIEW instead of Scoll View (by the way Scroll View is a child of VIEW), but that resulted in the app breaking (termination error).

Any advice would be appreciated.

+2  A: 

The easiest (and correct way) is to set an background image to your UIScrollView

UIImage *img = [UIImage imageNamed:@"image.png"];
[scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];
unset
Thanks. It works much better then the one i had, but both content and image scroll together. Anyway to keep the background image static and only move the scrollview content ?
Raymond Choong
A: 

Thanks. This was extremely helpful.

The one thing that I would add (pardon if it is obvious, but for people new to the platform (i.e., me), it took a bit of playing around).

I put the code from "unset"'s example after where I setup the contentSize=CGSizeMake in the viewDidLoad of the UIViewController , i.e.,:

// Setup the Scroll View
UIScrollView *tempScrollView=(UIScrollView *)self.view;
tempScrollView.contentSize=CGSizeMake(320, 720);

// Set Stationary Background, so that while the user scroll the background is
// fixed.
UIImage *img = [UIImage imageNamed:@"bg-body.jpg"];
[tempScrollView setBackgroundColor:[UIColor colorWithPatternImage:img]];

Then, I went into the ViewController .xib file and on the UIView that is inside of the UIScrollView, I set the background color to "Clear Color".

I sure that there is a way to do this through code at the same time as setting the background of the UIScrollView, but at least that is how you can do it through IB.

Jason

JasonBub