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.
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. (resize or setContentMode) Hope this helps
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);
)
I think this is help for someone. :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.