2012-04-20 172 views
2

我有一个真正有线的问题,我不知道如何解决它。我为UIButtons框架制作了动画,当我为它设置动画时,我希望按钮中的图像可以缩放到与按钮相同的尺寸。它在我的iPhone模拟器上无需执行任何操作。但是,当我在iPad模拟器上运行它时,它不会更改图像大小。我在几个地方添加了[[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill]代码,但它没有帮助。UIButton图像调整大小/缩放以适合

有谁知道什么是错的,以及如何解决这个问题?

//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; 
....... 

回答

24

尝试setBackgroundImage代替setImage,什么事都不做的形象。 (调整或setContentMode) 希望这有助于

+0

是的,它帮助,现在它的工作。谢谢 – Uffe 2012-04-20 12:58:30

+0

它的工作...谢谢你 – eurica 2014-06-20 08:21:24

0
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); 

如果不想显示标题然后删除最后一行(button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, 0);

我认为这是有人帮助。 :)