UIView的屬性contentMode用于當(dāng)視圖的bounds(邊界)發(fā)生變化時(shí)來(lái)確定視圖如何布局,也就是說(shuō)此屬性通常用于實(shí)現(xiàn)可調(diào)整大小的控件。
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,
};
設(shè)置UIImageView圖片常用的三個(gè)類(lèi)型:
- UIViewContentModeScaleToFill
通過(guò)更改內(nèi)容的橫縱比來(lái)實(shí)現(xiàn)內(nèi)容的縮放背传,以適應(yīng)自身的大小。UIImageView設(shè)置contentMode = ScaleToFill 會(huì)使圖片充滿(mǎn)容器忿峻,因?yàn)橐3謨?nèi)容適配容器個(gè)寬高掀亥,所以圖片在容器中會(huì)被拉伸;
- UIViewContentModeScaleAspectFit
通過(guò)保持縱橫比來(lái)縮放內(nèi)容以適應(yīng)視圖大小,視圖邊界的任何剩余區(qū)域都是透明的。保持長(zhǎng)寬比的前提下租冠,縮放圖片鹏倘,使得圖片在容器內(nèi)完整顯示出來(lái)。
-
UIViewContentModeScaleAspectFill
縮放內(nèi)容以保證填充視圖大小顽爹,可以剪裁部分內(nèi)容以填充視圖的邊界纤泵。在保持長(zhǎng)寬比的前提下,縮放圖片镜粤,使圖片充滿(mǎn)容器捏题,內(nèi)容四周可能會(huì)被剪切。
效果圖:
-
原圖效果:
原圖 -
ScaleToFill效果:
IMG_0047 -
AspectFit效果:
AspectFit AspectFill效果:
AspectFill