contentMode的描述

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啥时候起作用呢?

  • 1: 视图frame或bounds的高宽发生变化
  • 2: 赋给 view的transform属性的值带有scale

一: Content Modes

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

  • 1: UIViewContentModeScaleToFill: 改变内容的高宽比例,缩放内容,UIView中完整显示内容,填满UIView(Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.)

  • 2: UIViewContentModeScaleAspectFit: 保持内容的高宽比,缩放内容,完整显示内容,最大化填充UIview,没填充上的区域透明(Scales the content to fit the size of the view by maintaining the aspect ratio. Any remaining area of the view’s bounds is transparent.)

  • 3: UIViewContentModeScaleAspectFill: 保持内容高宽比,缩放内容,超出视图的部分内容会被裁减,填充UIView(Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.)

  • 4: UIViewContentModeRedraw: 当View的bounds改变,系统会调用setNeedsDisplay,重新绘制视图(The option to redisplay the view when the bounds change by invoking the setNeedsDisplay method)

  • 5: UIViewContentModeCenter: 不缩放,内容在视图中间(The option to center the content in the view’s bounds, keeping the proportions the same)

UIViewContentModeTop

The option to center the content aligned at the top in the view’s bounds.

 
UIViewContentModeBottom

The option to center the content aligned at the bottom in the view’s bounds.


UIViewContentModeLeft

The option to align the content on the left of the view.


UIViewContentModeRight

The option to align the content on the right of the view.


UIViewContentModeTopLeft

The option to align the content in the top-left corner of the view.

 
UIViewContentModeTopRight

The option to align the content in the top-right corner of the view.

 
UIViewContentModeBottomLeft

The option to align the content in the bottom-left corner of the view.

 
UIViewContentModeBottomRight

The option to align the content in the bottom-right corner of the view.

参考: https://stackoverflow.com/questions/4895272/difference-between-uiviewcontentmodescaleaspectfit-and-uiviewcontentmodescaletof

https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/WindowsandViews/WindowsandViews.html

你可能感兴趣的:(contentMode的描述)