Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scroll the content without scrolling the image in the background using UIScrollView?

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.

like image 592
Raymond Choong Avatar asked May 18 '10 08:05

Raymond Choong


3 Answers

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]];
like image 144
cem Avatar answered Nov 03 '22 14:11

cem


The easiest way is to set an UIImageView behind your UIScrollView(UIImageView and UIScrollView under UIView) and make your UIScrollView background color as clear color as mentioned by Janson. By this way you can scroll the content without scrolling the image in the background using UIScrollView.

like image 21
sps Avatar answered Nov 03 '22 12:11

sps


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

like image 28
JasonBub Avatar answered Nov 03 '22 12:11

JasonBub