如何缩放UIButton的imageView?

时间:2009-12-24 08:09:17

标签: ios uibutton scale

我使用[UIButton setImage:forState:]创建了一个名为“button”的UIButton实例。 button.frame大于图片的大小。

现在我想缩小此按钮的图像。我尝试更改button.imageView.framebutton.imageView.boundsbutton.imageView.contentMode,但似乎都无效。

任何人都可以帮我缩放UIButton的imageView吗?

我像这样创建了UIButton

UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];

我试图像这样缩放图像:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);

和此:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);

20 个答案:

答案 0 :(得分:87)

对于原始海报,这是我找到的解决方案:

commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

这将允许您的按钮水平缩放。还有一个垂直设置。

花了我几个小时来计算出那一个(该属性的命名非常不直观)所以我想分享。

答案 1 :(得分:61)

我遇到了类似的问题,我有一个背景图像(一个带边框)和一个自定义按钮的图像(标记)。我希望旗帜缩小并放在中心位置。我尝试更改imageView的属性,但没有成功,并且看到了这个图像 -

enter image description here

在我的实验中,我尝试过:

button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)

我达到了预期的结果:

enter image description here

答案 2 :(得分:31)

XIB文件中配置的添加方式。 如果您希望文本或图像是UIButton中的Scale full fill,则可以选择此选项。 它与代码相同。

UIButton *btn = [UIButton new];

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment   = UIControlContentVerticalAlignmentFill;

enter image description here

答案 3 :(得分:22)

使用

button.contentMode = UIViewContentModeScaleToFill;

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

更新

正如@Chris评论的那样,

  

要使其适用于setImage:forState:,您需要执行以下操作以水平缩放:myButton.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentFill;

答案 4 :(得分:15)

    UIButton *button= [[UIButton alloc] initWithFrame:CGRectMake(0,0,70,70)];
    button.buttonType = UIButtonTypeCustom;
    UIImage *buttonImage = [UIImage imageNamed:@"image.png"];

    UIImage *stretchableButtonImage = [buttonImage stretchableImageWithLeftCapWidth:12 topCapHeight:0]; 
    [button setBackgroundImage:stretchableButtonImage forState:UIControlStateNormal]; 

答案 5 :(得分:10)

我找到了这个解决方案。

1)对UIButton

的以下方法进行子类化
+ (id)buttonWithType:(UIButtonType)buttonType {
    MyButton *toReturn = [super buttonWithType:buttonType];
    toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return toReturn;
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return contentRect;
}

效果很好。

答案 6 :(得分:9)

奇怪,唯一对我有用的组合(iOS 5.1)是......

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

[button setImage:newImage forState:UIControlStateNormal];

答案 7 :(得分:7)

我刚遇到同样的问题,这个问题可能有答案:

Why does a custom UIButton image does not resize in Interface Builder?

基本上,使用backgroundimage属性,它会被缩放。

答案 8 :(得分:6)

Just do(From Design OR From Code):

来自设计

  1. 打开你的xib或故事板。
  2. 选择按钮
  3. 内部属性检查器(右侧)>在“控制”部分>选择最后第4个选项,包括水平和Verical。
  4. [对于第3点:更改水平和垂直对齐UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]

    来自代码

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
    button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;
    

答案 9 :(得分:5)

像这样可以解决你的问题:

+ (UIImage*)resizedImage:(UIImage*)image
{
 CGRect frame = CGRectMake(0, 0, 60, 60);
 UIGraphicsBeginImageContext(frame.size);
 [image drawInRect:frame];
 UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();

 return resizedImage;
}

答案 10 :(得分:5)

从我在这里阅读后做的小测试,取决于你是否使用setImage或setBackgroundImage,两者都做了相同的结果并拉伸图像

//for setBackgroundImage
 self.imageButton.contentMode = UIViewContentModeScaleAspectFill;
        [self.imageButton setBackgroundImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];

//for setImage
 self.imageButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
        self.imageButton.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
    [self.imageButton setImage:[UIImage imageNamed:@"imgFileName"] forState:UIControlStateNormal];

答案 11 :(得分:4)

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;

答案 12 :(得分:1)

来自@JosephH

contentHorizontalAlignment = UIControl.ContentHorizontalAlignment.fill contentVerticalAlignment = UIControl.ContentVerticalAlignment.fill

答案 13 :(得分:1)

故事板

您必须在身份检查器 - imageView.contentModel运行时属性中定义特定属性,其中您将值设置为相对于枚举的rawValue位置{ {1}}。 1表示 scaleAspectFit

Set button.imageView.contentMode in Storyboard

按钮的对齐方式,在属性检查器

Full alignment of button

答案 14 :(得分:1)

这也可以,因为backgroundImage会自动缩放

[button setBackgroundImage:image forState:UIControlStateNormal];

答案 15 :(得分:1)

我无法通过_imageView获得解决方案,但我可以使用CGContextRef来解决它。它使用UIGraphicsGetCurrentContext获取currentContextRef并在currentContextRef中绘制图像,然后缩放或旋转图像并创建新图像。但它并不完美。

代码:

-(UIImage*) scaleAndRotateImage:(UIImage*)photoimage width:(CGFloat)bounds_width height:(CGFloat)bounds_height;
{
    CGImageRef imgRef = photoimage.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);

    bounds.size.width = bounds_width;
    bounds.size.height = bounds_height;

    CGFloat scaleRatio = bounds.size.width / width;
    CGFloat scaleRatioheight = bounds.size.height / height;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = photoimage.imageOrientation;
    switch(orient)
    {
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid?image?orientation"];
            break;
    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft)
    {
        CGContextScaleCTM(context, -scaleRatio, scaleRatioheight);
        CGContextTranslateCTM(context, -height, 0);
    }
    else
    {
        CGContextScaleCTM(context, scaleRatio, -scaleRatioheight);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}

答案 16 :(得分:0)

Screenshot in XCode 8

在屏幕截图的左下角,您可以看到拉伸属性。尝试使用它们。

答案 17 :(得分:0)

我希望这可以对其他人有所帮助

快速3 +

button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.fill
button.contentVerticalAlignment =  UIControlContentVerticalAlignment.fill

答案 18 :(得分:0)

您可以使用UIButton的自定义类来修改imageView。请看我的回答。 https://stackoverflow.com/a/59874762/5693826

答案 19 :(得分:-1)

每个UIButton都有一个隐藏的UIImageView。所以我们必须像下面给出的方式设置内容模式......

[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];