我在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;
答案 0 :(得分:3)
您需要将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;
答案 1 :(得分:2)
您不能将imageView的帧作为图像的大小(因为图像太大)。根据需要设置imageview框架并设置图像内容模式。
您可能需要选择正确的图像内容模式
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,
};