一. iOS提供了的ContentMode有如下几种
其中默认mode是UIViewContentModeScaleToFill
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,
};
二. 煮几个栗子
UIViewContentModeScaleToFill
特点:
强行把原始图片塞进UIImageView设置的size里;
如果宽高比相同,等比例缩放;
如果宽高比不同,会变形塞入;
UIViewContentModeScaleAspectFit
特点:
等比例缩放图片以适应UIImageView的size,不会裁剪图片
图片的宽度或者高度(至少一个)恰好与视图的宽度或者高度相等;
UIViewContentModeScaleAspectFill
特点:
等比例缩放图片但会超出边界,不会裁剪图片
图片的宽度或者高度(至少一个)恰好与视图的宽度或者高度相等;
UIViewContentModeRedraw
效果同默认态UIViewContentModeScaleToFill,设置没什么特殊效果,而是告诉视图在每次设置或者更改frame的时候自动调用drawRect:方法
UIViewContentModeLeft 和 UIViewContentModeRight
效果为左对齐、右对齐;如上图对比