Iphone 带图像的UI按钮

Iphone 带图像的UI按钮,iphone,uibutton,Iphone,Uibutton,我有一个使用IB的按钮。现在我想以编程方式向按钮添加一个图像。我如何设置按钮的大小图像的大小,就像我们在IB布局->大小,以适应。我想用编程的方式来做 谢谢..示例代码 UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; UIImage *img1 = [UIImage imageNamed:@"image1.png"]; btn.frame = CGRectMake(20.0 , 270.0, img1.size.widt

我有一个使用IB的按钮。现在我想以编程方式向按钮添加一个图像。我如何设置按钮的大小图像的大小,就像我们在IB布局->大小,以适应。我想用编程的方式来做

谢谢..

示例代码

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img1 = [UIImage imageNamed:@"image1.png"];
btn.frame = CGRectMake(20.0 , 270.0, img1.size.width, img1.size.height);
[btn setImage:img1 forState:UIControlStateNormal]; 
UIImage *img2 = [UIImage imageNamed:@"image2.png"];
[btn setImage:img2 forState:UIControlStateHighlighted];
[btn setImage:img2 forState:UIControlStateSelected];
[btn addTarget:self action:@selector(Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

您可以使用
UIButton
setImage:forState:
方法向按钮添加图像,然后使用
UIView
contentMode
属性设置内容大小

示例如下所示:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:@"myImage.png"];

button.frame = CGRectMake(20, 100, img.size.width, img.size.height);

[button setImage:img forState:UIControlStateNormal];
[button setImage:img forState:UIControlStateHighlighted];
[button setImage:img forState:UIControlStateSelected];

button.contentMode = UIViewContentModeScaleToFill; //Look up UIViewContentMode in the documentation for other options

[self.view addSubview:button];
其中,
contentMode
的值可以是以下任一值

typedef enum {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
} UIViewContentMode;

我认为这可以帮助您使用UIButton(实际上是UIView)的sizeToFit方法。

谢谢您的分享!!对于从该解决方案进行复制和粘贴的人,他将
UIControlStateHighlighted
拼写错误。
typedef enum {
    UIViewContentModeScaleToFill,
    UIViewContentModeScaleAspectFit,      // contents scaled to fit with fixed aspect. remainder is transparent
    UIViewContentModeScaleAspectFill,     // contents scaled to fill with fixed aspect. some portion of content may be clipped.
    UIViewContentModeRedraw,              // redraw on bounds change (calls -setNeedsDisplay)
    UIViewContentModeCenter,              // contents remain same size. positioned adjusted.
    UIViewContentModeTop,
    UIViewContentModeBottom,
    UIViewContentModeLeft,
    UIViewContentModeRight,
    UIViewContentModeTopLeft,
    UIViewContentModeTopRight,
    UIViewContentModeBottomLeft,
    UIViewContentModeBottomRight,
} UIViewContentMode;