iOS - UIScrollView is not working (it doesn't

2019-02-01 20:45发布

I would like to display an image (width: 320 pixels, height: 1250 pixels) in an image view.

When I compile the application I get no scrolling at all. The view stays fixed.

What I did:

  1. Added an UIScrollView via Interface Builder to my view.
  2. Added an UIImageView via Interface Builder to my view.
  3. Verified that UIImageView is below UIScrollView in Interface Builder.
  4. Set the size of the UIScrollView with ViewDidLoad.

How do I do this?

Code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView.contentSize = CGSizeMake(320, 1250);
}

Screenshots:

ImageView:

Enter image description here

ScrollView:

Enter image description here Enter image description here

Enter image description here

17条回答
迷人小祖宗
2楼-- · 2019-02-01 21:12

The image view has to be a subview (so inside AND below) of the scrollview. From your description it seems they are paralell

查看更多
▲ chillily
3楼-- · 2019-02-01 21:17

I just have done the same task.. Try this one.....

scrollView.delegate = self;
scrollView.scrollEnabled = YES;

int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);     


int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];

for(int index=0; index < [imagesName count]; index++)
{       
    UIImageView *img = [[UIImageView alloc] init];
    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(5+xOffset, 0, 50, 50);
    NSLog(@"image: %@",[imagesName objectAtIndex:index]);
    img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
    [images insertObject:img atIndex:index];         
    scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110); 
    [scrollView addSubview:[images objectAtIndex:index]];

    xOffset += 70;
}

Also set this one....

imagesName = [[NSArray alloc]initWithObjects:@"image1.jpg",@"image2.jpg",@"image3.jpg",@"image4.jpg",@"image5.jpg",@"image6.png",@"image7.png",@"image9.png",nil];
    images = [[NSMutableArray alloc]init];
查看更多
\"骚年 ilove
4楼-- · 2019-02-01 21:17

You forgot one line. Add this to your view load function:

[scrollView setScrollEnabled:YES];
查看更多
放荡不羁爱自由
5楼-- · 2019-02-01 21:19

Assuming that scrollView is a subview of view and fills it entirely you can use the following in viewDidLoad:

  [scrollView setContentSize: CGSizeMake(self.view.frame.size.width, self.view.frame.size.height)];

I had a UIScrollView that was not scrolling and this allowed it to scroll.

查看更多
狗以群分
6楼-- · 2019-02-01 21:20

You could try disabling AutoLayout. In XCode 5 I tested all the above answers and I could only scroll it by disabling autolayout and activating all autosizing masks under the Size Inspector. The following code was used too:

self.scrollView.contentSize = CGSizeMake(320, 900);
self.scrollView.delegate = self;
self.scrollView.scrollEnabled = YES;
self.scrollView.frame = self.view.frame;
查看更多
Fickle 薄情
7楼-- · 2019-02-01 21:22

Sometimes autoLayout checkbox is ticked in the xib. That also creates this issue in xcode 5. Which makes the UIScrollView scrolling off.

查看更多
登录 后发表回答