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

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

I use IB and set the FileOwner View to a scroll list (Image View is a child of the scroll). I made an image height of 960 pixels.

I also set the scroll content size in the vierController to which this UIView belongs

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

My problem is that only the image appears along with the content.

I tried to take out the settings in viewDidLoad, but scrolling ceases to function.

I also tried changing the location of the image and placing it under VIEW instead of Scoll View (by the way, Scroll View is a child of VIEW), but this led to a violation of the application (completion error).

Any advice would be appreciated.

+4
source share
3 answers

The easiest (and the right way) is to set the background image to your UIScrollView

UIImage *img = [UIImage imageNamed:@"image.png"]; [scrollView setBackgroundColor:[UIColor colorWithPatternImage:img]]; 
+9
source

The easiest way is to set the UIImageView behind your UIScrollView (UIImageView and UIScrollView under the UIView) and make the background color of the UIScrollView a pure color, as Janson mentioned. This way you can scroll through the contents without scrolling the image in the background using UIScrollView.

+4
source

Thanks. It was very helpful.

The only thing I would add (sorry, if this is obvious, but for people new to the platform (i.e. me), it played a little).

I put the code from the "unset" example after I set contentSize = CGSizeMake to viewDidLoad of the UIViewController, that is,:

 // 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 in the UIView, which is inside the UIScrollView, I set the background color to "Clear Color".

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

Jason

+1
source

Source: https://habr.com/ru/post/1310086/


All Articles