我使用[UIButton setImage:forState:]
创建了一个名为"button“的带有图像的UIButton实例。button.frame大于图像的大小。
现在我想缩小这个按钮的图像。我尝试更改button.imageView.frame
、button.imageView.bounds
和button.imageView.contentMode
,但似乎都无效。
有人能帮我扩展一下UIButton
的imageView吗?
我创建的UIButton
是这样的:
UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];
我试着像这样缩放图像:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);
还有这个:
button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);
发布于 2013-09-03 16:59:29
我也遇到过类似的问题,我有一个背景图像(带边框)和一个自定义按钮的图像(标志)。我希望旗帜缩小并居中。我尝试更改imageView的属性,但没有成功,看到了这个图像--
在我的实验中,我尝试了:
button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)
我达到了预期的结果,如:
发布于 2015-12-16 12:27:57
在XIB文件中配置的一种添加方式。如果您希望文本或图像在UIButton中按全比例填充,可以选择此选项。代码也是一样的。
UIButton *btn = [UIButton new];
btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
发布于 2009-12-24 16:23:10
UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,70)];
button.buttonType = UIButtonTypeCustom;
UIImage *buttonImage = [UIImage imageNamed:@"image.png"];
UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0];
[button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal];
https://stackoverflow.com/questions/1957317
复制相似问题