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
根据视图的比例去拉伸图片内容。

这篇博客详细解析了UIView的contentMode属性,通过不同的UIViewContentMode设置,可以改变视图内容的显示方式,包括按比例填充、适应、居中、重绘等,确保图片在视图中得到适当的展示。
最低0.47元/天 解锁文章
32万+

被折叠的 条评论
为什么被折叠?



