xcode UIButton 图像调整大小/缩放以适合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10246515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIButton image resize/scale to fit
提问by Uffe
I have a really wired problem and i don't know how to solve it. I'm animating a UIButtons frame and when i animate it, i want the image in the button to scale to the same size as the button. It works without doing anything on my iPhone simulator. But when i run it on the iPad simulator it won't change the image size. I have added [[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill] code on several places but it did not help.
我有一个非常有线的问题,我不知道如何解决它。我正在为 UIButtons 框架设置动画,当我为它设置动画时,我希望按钮中的图像缩放到与按钮相同的大小。它无需在我的 iPhone 模拟器上执行任何操作即可工作。但是当我在 iPad 模拟器上运行它时,它不会改变图像大小。我在几个地方添加了 [[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill] 代码,但没有帮助。
Do anyone know what is wrong and how to fix this problem?
有谁知道出了什么问题以及如何解决这个问题?
//Code to set the image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_small",
[[itemsForRound objectAtIndex:[[guessItems objectAtIndex:0] intValue]] valueForKey:@"Name"]]];
[self.gissaImg1 setImage:image forState:UIControlStateNormal];
[self.gissaImg1 setImage:image forState:UIControlStateHighlighted];
//Code to animate button
CGRect animateToSpot = (isIphone) ? CGRectMake(0, 0, 480, 320) : CGRectMake(0, 0, 1024, 768);
[self performSelector:@selector(PlayCorrectSound:) withObject:nil afterDelay:1];
[[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill];
[UIView animateWithDuration:1.5 delay:0 options:(UIViewAnimationOptionAllowUserInteraction | UIViewAnimationCurveLinear) animations:^{
switch (index) {
case 0:
[[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill];
[self.gissaImg1 setFrame:animateToSpot];
[self.view bringSubviewToFront:gissaImg1];
break;
.......
回答by ultragtx
Try setBackgroundImage
instead of setImage
, and do nothing to the image. (resize or setContentMode)
Hope this helps
尝试setBackgroundImage
代替setImage
,不对图像做任何事情。(调整大小或 setContentMode)希望这有帮助
回答by Adarsh G J
UIImage *thubNailImage=[UIImage imageNamed:@"thumImage"];
[button.imageView setContentMode:UIViewContentModeScaleAspectFill];
CGSize imageSize=thubNailImage.size;
[button setImage:thubNailImage forState:UIControlStateNormal];
button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, 0);
If you don't want to show the title then remove the last line ( button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, 0);
)
如果您不想显示标题,请删除最后一行 ( button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, 0);
)
I think this is help for someone. :)
我认为这是对某人的帮助。:)