82

我使用[UIButton setImage:forState:]. button.frame 大于图像的大小。

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

任何人都可以帮我缩放 aUIButton的 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);
4

20 回答 20

94

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

commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

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

我花了几个小时才弄明白这个(属性的命名非常不直观)所以我想分享一下。

于 2011-11-18T08:56:24.387 回答
68

我遇到了类似的问题,我有一个背景图像(一个带边框)和一个自定义按钮的图像(标志)。我希望将标志按比例缩小并居中。我尝试更改 imageView 的属性,但没有成功并且看到了这张图片——

在此处输入图像描述

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

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

我达到了预期的结果:

在此处输入图像描述

于 2013-09-03T08:59:28.927 回答
48

XIB 文件中配置的一种附加方式。如果您希望文本或图像在 UIButton 中按比例完全填充,则可以选择此选项。这与代码相同。

UIButton *btn = [UIButton new];

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

在此处输入图像描述

于 2015-12-16T04:27:57.253 回答
24

use

button.contentMode = UIViewContentModeScaleToFill;

not

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

Update:

As @Chris commented,

To get this to work for setImage:forState:, you need to do the following to scale horizontally: myButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;

于 2010-04-10T15:09:05.857 回答
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]; 
于 2009-12-24T08:23:09.903 回答
10

我找到了这个解决方案。

1)子类化以下方法UIButton

+ (id)buttonWithType:(UIButtonType)buttonType {
    MyButton *toReturn = [super buttonWithType:buttonType];
    toReturn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    return toReturn;
}

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

而且效果很好。

于 2010-06-21T17:40:02.070 回答
9

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

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

[button setImage:newImage forState:UIControlStateNormal];

于 2012-04-02T17:43:20.837 回答
8

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

为什么自定义 UIButton 图像不会在 Interface Builder 中调整大小?

本质上,请改用 backgroundimage 属性,它会被缩放。

于 2010-09-14T05:56:27.530 回答
8

只是做(从设计或从代码):

从设计

  1. 打开您的 xib 或情节提要。
  2. 选择按钮
  3. 内部属性检查器(右侧)>在“控制”部分>选择水平和垂直的最后第四个选项

[对于第 3 点:将水平和垂直对齐更改为UIControlContentHorizontalAlignmentFill and UIControlContentVericalAlignmentFill]

从代码

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;
于 2015-09-11T08:24:42.837 回答
5

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

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

 return resizedImage;
}
于 2010-03-17T03:26:23.577 回答
5

从我在阅读here后所做的小测试中,取决于您使用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];
于 2013-11-18T23:26:44.207 回答
4
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;
于 2012-11-01T13:01:00.893 回答
4

故事板

您必须在身份检查器的Runtime Attributes –中定义特定属性,您可以在其中设置相对于enum 位置的值。1 表示scaleAspectFitimageView.contentModelrawValueUIViewContentMode

在 Storyboard 中设置 button.imageView.contentMode

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

按钮完全对齐

于 2017-10-02T20:33:48.140 回答
2

我希望这对其他人有帮助:

斯威夫特 3+

button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.fill
button.contentVerticalAlignment =  UIControlContentVerticalAlignment.fill
于 2019-05-02T22:09:14.110 回答
2

这也将起作用,因为 backgroundImage 会自动缩放

[button setBackgroundImage:image forState:UIControlStateNormal];

于 2016-03-16T22:17:48.087 回答
2

来自@JosephH

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

于 2020-12-28T01:15:15.957 回答
1

我无法获得 _imageView 使用的解决方案,但我可以使用 aCGContextRef来解决它。它使用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;
}
于 2009-12-25T03:16:37.637 回答
0

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

于 2020-01-23T09:07:31.643 回答
0

XCode 8 中的屏幕截图

在屏幕截图的左下方,您可以看到拉伸属性。尝试使用这些。

于 2017-08-15T19:29:30.203 回答
-1

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

[[btn imageView] setContentMode: UIViewContentModeScaleAspectFit];
[btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
于 2017-07-15T12:12:36.940 回答