設(shè)置圖片顯示的狀態(tài),有這么幾個屬性.根據(jù)情況具體使用
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,
};
如果我們使用的是使用的 imageView ,
imageView.contentMode = UIViewContentModeScaleAspectFill;
這樣就能達到我們的效果,但是如果使用的是button.要滿足三個條件
用 button.imageView.contentMode 而不是 button.contentMode
打開 clipsToBounds 屬性. 設(shè)置為 YES
設(shè)置的是 image 而不是 backGroundImage ,設(shè)置 backGroundImage 沒有效果.
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.clipsToBounds = YES;
[button sd_setImageWithURL:[NSURL URLWithString:imag1] forState:UIControlStateNormal];