UIViewContentMode各类型效果

UIViewContentMode

typedef enum {
    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,
} UIViewContentMode;  

 
contentMode 是UIView中的属性

@property(nonatomic)                 UIViewContentMode contentMode;                // default is UIViewContentModeScaleToFill

默认值是 0,也就是:

UIViewContentModeScaleToFill

一个个说明:

  • UIViewContentModeScaleToFill:表示完全填充在 frame 里。
  • UIViewContentModeScaleAspectFit:保持比例,都在 frame 内。
  • UIViewContentModeScaleAspectFill:保持比例,填满但 frame 外也有。
  • UIViewContentModeRedraw:啥意思我还不懂。

其他的是相似的,好理解:

  • UIViewContentModeCenter:这个 image 的中心与 frame 的中心重合。
  • UIViewContentModeTop:这个 image 的上边缘与 frame 的上边缘重合。
  • UIViewContentModeBottom:这个 image 的下边缘与 frame 的下边缘重合。
  • UIViewContentModeLeft:这个 image 的左边缘与 frame 的左边缘重合。
  • UIViewContentModeRight:这个 image 的右边缘与 frame 的右边缘重合。
  • UIViewContentModeTopLeft:类似。
  • UIViewContentModeTopRight:类似。
  • UIViewContentModeBottomLeft:类似。
  • UIViewContentModeBottomRight:类似。
注意以上几个常量,凡是没有带Scale的,当图片尺寸超过 
ImageView尺寸时,只有部分显示在ImageView中。UIViewContentModeScaleToFill属性会导致图片变形。UIViewContentModeScaleAspectFit会保证图片比例不变,而且全部显示在ImageView中,这意味着ImageView会有部分空白。UIViewContentModeScaleAspectFill也会证图片比例不变,但是是填充整个ImageView的,可能只有部分图片显示出来。 
原文地址:https://www.cnblogs.com/56ik/p/5136725.html