标签:
在某些情况下,我们使用的UIButton的背景图片不一定就是标准的尺寸,有时会偏大,那么怎么办?
这个比较简单直接设置 : self.imageView.contentMode = UIViewContentModeScaleAspectFill;
但是问题来了,如果图片尺寸比较button的bounds 小的话,他不会撑满整个控件默认会居中显示,有些时候这并不是我们想要的效果。有时我们希望他能撑满整个button。
1 |
- (CGRect)imageRectForContentRect:(CGRect)bounds{ |
2 |
return CGRectMake(0.0, 0.0, self.size.width, self.size.height); |
3 |
} |
那么这样处理即可:
继承UIButton,重写 imageRectForContentRect 方法返回button的bounds
btn.imageView setContentMode:UIViewContentModeScaleAspectFill];
下面方法自己可以试下, 你就知效果了...
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,
};
UIButton设置imgae图片自适应button的大小且不变形
标签:
原文地址:http://www.cnblogs.com/OIMM/p/5576853.html