UIView contentMode
Content Modes
contentMode是UIView的屬性,這個屬性的值決定了,當視圖的幾何形狀變化時如何復用它的內容。當視圖第一次展示前,它會將自己的內容渲染成一張底層的bitmap. 然后視圖的幾何變化都不會使bitmap重新生成。而視圖contentMode屬性的值決定了bitmap是否縮放、位置在哪兒(固定在左邊、右邊、上面、下面、居中)。默認情況下,contentMode的值是
contentMode啥時候起作用呢?
- 視圖frame或bounds的高寬發生變化
- 賦給 view的transform屬性的值帶有scale
UIViewContentModeScaleToFill
Scales the content to fit the size of itself by changing the aspect ratio of the content if necessary.
改變內容的高寬比例,縮放內容,UIView中完整顯示內容,填滿UIView,
UIViewContentModeScaleAspectFit
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.
保持內容的高寬比,縮放內容,完整顯示內容,最大化填充UIview,沒填充上的區域透明
UIViewContentModeScaleAspectFill
Scales the content to fill the size of the view. Some portion of the content may be clipped to fill the view’s bounds.
保持內容高寬比,縮放內容,超出視圖的部分內容會被裁減,填充UIView
UIViewContentModeRedraw
The option to redisplay the view when the bounds change by invoking the setNeedsDisplay method.
當View的bounds改變,系統會調用setNeedsDisplay,重新繪制視圖
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.