如何缩放UIButton的imageView?

vcL*_*wei 79 scale uibutton ios

我用图像创建了一个名为"button"的UIButton实例[UIButton setImage:forState:].button.frame大于图像的大小.

现在我想缩小此按钮的图像.我试图改变button.imageView.frame,button.imageView.boundsbutton.imageView.contentMode,但都显得无效.

任何人都可以帮助我缩放图像UIButton视图吗?

我创建了UIButton这样的:

UIButton *button = [[UIButton alloc] init];
[button setImage:image forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

我试图像这样缩放图像:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.bounds = CGRectMake(0, 0, 70, 70);
Run Code Online (Sandbox Code Playgroud)

还有这个:

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
button.imageView.frame = CGRectMake(0, 0, 70, 70);
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 87

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

commentButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)

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

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


Hit*_*iya 63

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

在此输入图像描述

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

button.imageEdgeInsets = UIEdgeInsetsMake(kTop,kLeft,kBottom,kRight)
Run Code Online (Sandbox Code Playgroud)

我达到了预期的结果:

在此输入图像描述


Lin*_*yen 35

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

UIButton *btn = [UIButton new];

btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
btn.contentVerticalAlignment   = UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 22

使用

button.contentMode = UIViewContentModeScaleToFill;
Run Code Online (Sandbox Code Playgroud)

button.imageView.contentMode = UIViewContentModeScaleAspectFit;
Run Code Online (Sandbox Code Playgroud)

更新:

正如@Chris评论的那样,

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

  • 要使其适用于setImage:forState:,您需要执行以下操作以水平扩展:myButton.contentHorizo​​ntalAlignment = UIControlContentHorizo​​ntalAlignmentFill; (7认同)
  • 这与[button setBackgroundImage:forState:]结合使用效果很好.谢谢. (3认同)

EEE*_*EEE 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]; 
Run Code Online (Sandbox Code Playgroud)


mar*_*cio 10

我找到了这个解决方案

1)子类以下方法 UIButton

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

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return contentRect;
}
Run Code Online (Sandbox Code Playgroud)

它运作良好.


Hlu*_*ung 9

奇怪,唯一适合我的组合(iOS 5.1)是......

button.imageView.contentMode = UIViewContentModeScaleAspectFit;

[button setImage:newImage forState:UIControlStateNormal];


Jos*_*phH 7

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

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

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


San*_* SM 6

只做(来自设计或来自代码):

来自设计

  1. 打开你的xib或故事板.
  2. 选择按钮
  3. 内部属性检查器(右侧)>在"控制"部分> 为水平和Verical 选择最后的第4个选项.

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

从代码

button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment =  UIControlContentVerticalAlignmentFill;
Run Code Online (Sandbox Code Playgroud)


小智 5

这样可以解决你的问题:

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

 return resizedImage;
}
Run Code Online (Sandbox Code Playgroud)


use*_*951 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];
Run Code Online (Sandbox Code Playgroud)


dim*_*iax 5

故事板

\n

您必须在身份检查器\xe2\x80\x93的运行时属性中定义特定属性,在其中相对于enum 的位置设置值。1 表示scaleAspectFitimageView.contentModelrawValueUIViewContentMode

\n

在Storyboard中设置button.imageView.contentMode

\n

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

\n

按钮完全对齐

\n