我有一个没有文字的UIButton,有2个我想使用的图像(一个用于正常状态,另一个用于选择状态).图像小于按钮大小.
如何确保在绘制按钮时不缩放任何图像?设置imageView属性仅在正常状态下正确更改比例,而不是为选定状态更改比例.
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;
Run Code Online (Sandbox Code Playgroud)
Phi*_*rin 10
假设你有图像的高度和宽度,你可以这样做:
int topBottom = (button.frame.size.height - imageHeight) / 2;
int leftRight = (button.frame.size.width - imageWidth) / 2;
button.imageEdgeInsets = UIEdgeInsetsMake(topBottom,leftRight,topBottom,leftRight);
Run Code Online (Sandbox Code Playgroud)
然后您不需要设置contentMode/scalefactor.
| 归档时间: |
|
| 查看次数: |
3410 次 |
| 最近记录: |