【问题标题】:Size of UIScrollView and Size of ImagesUIScrollView 的大小和图像的大小
【发布时间】:2015-04-21 19:08:04
【问题描述】:

我创建了这个动态加载图像的滚动视图。但是我无法让图像一张一张地覆盖屏幕(应该适应 iPhone/iPad)。并且图像的高度也损坏了(见附图)

我做错了什么?

图片

加载图片的来源:

 for (int i=0; i < numberOfImages; i++) {

        // create image
        NSString *imageName = [NSString stringWithFormat: imageArray[i], i+1];
        UIImage *image = [UIImage imageNamed:imageName];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

        // put image on correct position
        CGRect rect = imageView.frame;
        rect.origin.x = currentX + self.scrollView.frame.size.width/2;
        imageView.frame = rect;

        // update currentX
        currentX += imageView.frame.size.width + self.scrollView.frame.size.width;

        [self.scrollView addSubview:imageView];
        [imageView release];
    }

    self.scrollView.contentSize = CGSizeMake(currentX, self.view.frame.size.height);

    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
    [self.view addSubview:scrollView];
    [self.scrollView release];

【问题讨论】:

  • 您能否提供更多关于您希望如何定位图像的详细信息?看起来你有一个页面控件,所以你希望图像水平并排?
  • @pteofil 正确!图像应该居中,并且只有一个可见的 uiscrollview 应该在某一时刻可见。在 iPhone 和 iPad 上应该显示相同。

标签: ios iphone xcode uiscrollview


【解决方案1】:

这应该适合你:

for (int i = 0; i < numberOfImages; i++) {

    // create image
    NSString *imageName = [NSString stringWithFormat: imageArray[i], i+1];
    UIImage *image = [UIImage imageNamed:imageName];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    imageView.contentMode = UIViewContentModeCenter;

    // put image on correct position
    CGRect rect = self.scrollView.bounds;
    rect.origin.x = currentX;
    imageView.frame = rect;

    // update currentX
    currentX += self.scrollView.frame.size.width;

    [self.scrollView addSubview:imageView];
    [imageView release];
}

self.scrollView.contentSize = CGSizeMake(currentX, self.scrollView.frame.size.height);

您还可以将 scrollView 分页属性设置为 yes,这样它就会逐页滚动。

【讨论】:

  • 我不得不使用图像框架,并且正在努力将图像分开并居中,但这正是我所需要的。谢谢!赏金来了! :)
【解决方案2】:

您应该使用具有水平滚动功能的 UICollectionView 而不是 scrollView,并且您可以从集合视图实现委托以提供具有所需尺寸的单元格。实现会更加简单明了。

【讨论】:

  • 在我设置 pteofils 对赏金的回答之前,必须先调查一下 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多