Question

I want to have a UIScrollView on a panoramic image so that you can pan across the image horizontally. For some reason, the code I'm using does not allow the user to scroll horizontally.

Why won't it scroll horizontally?

Here is the code:

  [scrollView setFrame:CGRectMake(0,160,1338,269)];
  scrollView.scrollEnabled = YES;
    scrollView.userInteractionEnabled = YES;

UIImageView *panorama = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1338, 269)];
panorama.image = [UIImage imageNamed:@"Panorama.png"];

scrollView.contentSize = CGSizeMake(320, 269);
[scrollView addSubview:panorama];
[self.view addSubview:scrollView];
Was it helpful?

Solution

The scrollView's frame is stationary, the contentSize is the one that allows the scrolling.

scrollView.contentSize = CGSizeMake(320, 269);

Should be replaced with

scrollView.contentSize = CGSizeMake(1338, 269);

To allow a large contentSize to be scrolled through. The frame should be smaller.

scrollView.frame = CGRectMake(0,0,320, 269);

Think of the frame as the area that you see on the screen at any given time. Think of the contentSize as the area that the scrollView is capable of showing (much larger).

OTHER TIPS

You are doing it other way around. Scroll view's content size must be set to the size of the large image you are trying to scroll.

Change contentsize to size of the uiimageview and change scrollview frame to 320,269

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top