简体   繁体   English

UIButton图像调整大小/缩放以适应

[英]UIButton image resize/scale to fit

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. 我正在设置UIButtons框架的动画,当我为它设置动画时,我希望按钮中的图像缩放到与按钮相同的大小。 It works without doing anything on my iPhone simulator. 它可以在我的iPhone模拟器上无需任何操作。 But when i run it on the iPad simulator it won't change the image size. 但是当我在iPad模拟器上运行它时,它不会改变图像大小。 I have added [[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill] code on several places but it did not help. 我在几个地方添加了[[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;
.......

Try setBackgroundImage instead of setImage , and do nothing to the image. 尝试使用setBackgroundImage而不是setImage ,并且setImage图像执行任何操作。 (resize or setContentMode) Hope this helps (resize或setContentMode)希望这会有所帮助

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. 我认为这对某人有帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM