【问题标题】:I need to make my UIImageView fit inside my UIScrollView without panning. [iOS]我需要让我的 UIImageView 适合我的 UIScrollView 而不平移。 [iOS]
【发布时间】:2014-09-19 23:10:23
【问题描述】:

我在 iOS UIScrollView 中有一个图像,它只占用了大约 1/3 的总屏幕空间。图像太大,所以 scrollView 允许在图像周围平移。我希望图像足够小,这样就不需要平移来查看整个图像。如何调整图像大小以使其适合滚动视图?

这是我用来设置图像视图和滚动视图的代码。

UIImage *image = [UIImage imageNamed:@"banner.png"];
self.imageView = [[UIImageView alloc] initWithImage:image];
self.imageView.frame = (CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=image.size};
[self.scrollView addSubview:self.imageView];

self.scrollView.contentSize = image.size;

【问题讨论】:

    标签: ios objective-c uiscrollview uiimageview


    【解决方案1】:

    你不能不把imageView的frame作为图片的大小(因为图片太大了)。根据需要设置图像视图的框架并设置图像内容模式。

    您可能需要选择正确的图像内容模式

    self.imageView.contentMode = (Pick right content mode you need)
    
    typedef NS_ENUM(NSInteger, UIViewContentMode) {
        UIViewContentModeScaleToFill,
        UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
        UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
        UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
        UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
        UIViewContentModeTop,
        UIViewContentModeBottom,
        UIViewContentModeLeft,
        UIViewContentModeRight,
        UIViewContentModeTopLeft,
        UIViewContentModeTopRight,
        UIViewContentModeBottomLeft,
        UIViewContentModeBottomRight,
    };
    

    【讨论】:

      【解决方案2】:

      您需要将 ImageView 的大小设置为等于 scrollView 的大小。并将imageView的内容模式设置为缩放填充 例如:

      UIImage *image = [UIImage imageNamed:@"banner.png"];
      self.imageView = [[UIImageView alloc] initWithImage:image]
      self.imageView.frame = CGRectMake (0,0,self.scrollView.frame.size.width,self.scrollView.frame.size.height);
      
      self.imageView.contentMode = UIViewContentModeScaleToFill;
      [self.scrollView addSubview:self.imageView];
      self.scrollView.contentSize = self.imageView.frame.size;
      self.scrollView.bounces = NO;
      

      【讨论】:

      • 这工作除了一个问题。 self.scrollView.contentSize = self.imageView.size;导致构建失败。错误是“在‘UIImageView’类型的对象上找不到属性‘大小’”。
      • self.scrollView.contentSize = self.imageView.frame.size;对不起,我的错 :) 不要忘记投票 :)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      • 2013-08-11
      • 1970-01-01
      • 2021-05-05
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多