Iphone 防止为UIButton调整UIImage大小
我有一个没有文本的UIButton,有两个我想使用的图像(一个用于正常状态,另一个用于选定状态)。图像小于按钮大小 如何确保在绘制按钮时两个图像都不缩放?设置imageView属性仅会正确更改正常状态下的比例,而不会更改选定状态下的比例Iphone 防止为UIButton调整UIImage大小,iphone,ios5,Iphone,Ios5,我有一个没有文本的UIButton,有两个我想使用的图像(一个用于正常状态,另一个用于选定状态)。图像小于按钮大小 如何确保在绘制按钮时两个图像都不缩放?设置imageView属性仅会正确更改正常状态下的比例,而不会更改选定状态下的比例 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setImage:imageNormal forState:UIControlStateNorma
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:imageNormal forState:UIControlStateNormal];
[button setImage:imageSelected forState:UIControlStateSelected];
// this shows the correct scale in normal mode but not when button is tapped
button.imageView.contentScaleFactor = 1.0;
button.imageView.contentMode = UIViewContentModeCenter;
假设您具有图像的高度和宽度,则可以执行以下操作:
int topBottom = (button.frame.size.height - imageHeight) / 2;
int leftRight = (button.frame.size.width - imageWidth) / 2;
button.imageEdgeInsets = UIEdgeInsetsMake(topBottom,leftRight,topBottom,leftRight);
然后就不需要设置contentMode/scalefactor了。注意!!!默认情况下,仅拉伸背景图像以匹配帧。如果要居中,请使用“图像”而不是背景图像