iOS【UIView】contentMode

contentMode是UIView的属性(如下图),这个属性的值决定了,当视图的几何形状变化时如何复用它的内容。当视图第一次展示前,它会将自己的内容渲染成一张底层的bitmap. 然后视图的几何变化都不会使bitmap重新生成。而视图contentMode属性的值决定了bitmap是否缩放、位置在哪儿(固定在左边、右边、上面、下面、居中)。默认情况下,contentMode的值是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,
};

contentMode在以下两种情况下会起作用:
1.视图frame或bounds的高宽发生变化
2.赋给 view的transform属性的值带有scale

UIViewContentModeScaleToFill:改变内容的高宽比例,缩放内容,UIView中完整显示内容,填满UIView
UIViewContentModeScaleAspectFit:保持内容的高宽比,缩放内容,完整显示内容,最大化填充UIview,没填充上的区域透明
UIViewContentModeScaleAspectFill:保持内容高宽比,缩放内容,超出视图的部分内容会被裁减,填充UIView,需要把View的 clipsToBounds 设置为YES;
UIViewContentModeRedraw:当View的bounds改变,系统会调用setNeedsDisplay,重新绘制视图
UIViewContentModeCenter:不缩放,内容在视图中间

效果图如下:

1.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容