【问题标题】:UIButton image resize/scale to fitUIButton 图像调整大小/缩放以适应
【发布时间】:2012-05-02 01:20:34
【问题描述】:

我有一个非常棘手的问题,我不知道如何解决它。我正在为 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;
.......

【问题讨论】:

    标签: objective-c ios xcode ios5 xcode4


    【解决方案1】:
    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);)

    我认为这是对某人的帮助。 :)

    【讨论】:

      【解决方案2】:

      尝试使用setBackgroundImage 而不是setImage,并且不要对图像执行任何操作。 (调整大小或设置内容模式) 希望这会有所帮助

      【讨论】:

        猜你喜欢
        • 2015-07-16
        • 1970-01-01
        • 2023-01-26
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 2012-09-15
        • 2018-08-18
        • 2021-07-01
        相关资源
        最近更新 更多