UIView有个UIViewContentMode类型的属性contentMode,可以通过它来修改视图的内容显示模式。
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,
};
实例代码:
CGRect rect = self.view.frame;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:rect];
imageView.contentMode = UIViewContentModeTop;
imageView.image = [UIImage imageNamed:@"demoImage"];
[self.view addSubview:imageView];
UIViewContentModeScaleToFill
根据视图的比例去拉伸图片内容。