簡體   English   中英

UIButton圖像調整大小/縮放以適應

[英]UIButton image resize/scale to fit

我有一個非常有線的問題,我不知道如何解決它。 我正在設置UIButtons框架的動畫,當我為它設置動畫時,我希望按鈕中的圖像縮放到與按鈕相同的大小。 它可以在我的iPhone模擬器上無需任何操作。 但是當我在iPad模擬器上運行它時,它不會改變圖像大小。 我在幾個地方添加了[[gissaImg1 imageView] setContentMode:UIViewContentModeScaleToFill]代碼,但它沒有幫助。

有誰知道什么是錯的,以及如何解決這個問題?

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

嘗試使用setBackgroundImage而不是setImage ,並且setImage圖像執行任何操作。 (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);

如果您不想顯示標題,則刪除最后一行( button.titleEdgeInsets = UIEdgeInsetsMake(0, -imageSize.width, 0, 0);

我認為這對某人有幫助。 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM