content
@property(nonatomic) UIViewContentMode contentMode; // default is UIViewContentModeScaleToFill
@property(nonatomic) CGRect contentStretch NS_DEPRECATED_IOS(3_0,6_0) __TVOS_PROHIBITED; // animatable. default is unit rectangle {{0,0} {1,1}}. Now deprecated: please use -[UIImage resizableImageWithCapInsets:] to achieve the same effect.
UIViewContentMode
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,
};
contentMode属性在UIImageView中应用非常多,用来设置UIImageView的显示方式,contentMode的解释如下:
- UIViewContentModeScaleToFill:content缩放,改变比例,content和view完全重叠
- UIViewContentModeScaleAspectFit:content缩放,不改变比例,wRatio = view.bounds.width / content.width,hRatio = view.bounds.height / content.height,取ratio = min(wRatio, hRatio),然后content.width = content.width * ratio,content.height = content.height * ratio,view.bounds的空余部分透明,content的中心位置和view的中心位置重叠
- UIViewContentModeScaleAspectFill:content缩放,不改变比例,wRatio = view.bounds.width / content.width,hRatio = view.bounds.height / content.height,取ratio = max(wRatio, hRatio),然后content.width = content.width * ratio,content.height = content.height * ratio,content的中心位置和view的中心位置重叠,content大于view.bounds的部分是否裁剪遵循clipsToBounds设置
- UIViewContentModeRedraw:content缩放,改变比例使content和view完全重叠,当bounds改变时重绘,即调用setNeedsDisplay
- UIViewContentModeCenter:content不缩放,content的中心位置和view的中心位置重叠
- UIViewContentModeTop:content不缩放,content的顶部中间位置和view的顶部中间位置重叠
- UIViewContentModeBottom:content不缩放,content的底部中间位置和view的底部中间位置重叠
- UIViewContentModeLeft:content不缩放,content的左边中间位置和view的左边中间位置重叠
- UIViewContentModeRight:content不缩放,content的右边中间位置和view的右边中间位置重叠
- UIViewContentModeTopLeft:content不缩放,content的左上角位置和view的左上角位置重叠
- UIViewContentModeTopRight:content不缩放,content的右上角位置和view的右上角位置重叠
- UIViewContentModeBottomLeft:content不缩放,content的左下角位置和view的左下角位置重叠
- UIViewContentModeBottomRight:content不缩放,content的右下角位置和view的右下角位置重叠
注:如果clipsToBounds为NO,在UI上显示的是最终的content范围,即使bounds范围小于content范围,如果clipsToBounds为YES,则content显示范围不会超出bounds