在代码中为UIButton设置图像


196

如何在代码中为UIButton设置图像?

我有这个:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self action:@selector(goToOne) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btnTwo];

但看不到会为它设置图像的内容。

Answers:


398

物镜

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];

迅捷5.1

let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: .normal)

1
作为旁注:这将显示图像,但按钮标题文本将被隐藏。
leviathan

这是完全可以正常工作的代码,但是我正在通过解析获取图像,因此如何在UIImage情况下为我工作此代码,从而做到这一点:-largePick.image = aNewsInfo.smallImageData; 我该如何使用UIButton做到这一点?您能帮我吗?
user755278 2011年

1
@slcott,文档在哪里注明?我看不到任何建议不要setImage使用的地方。(在我看来,你是混淆UIButton.setImage使用UITableViewCell.image,这因为你说过时的属性。)
柯克沃尔

12
注意:如果你使用别的比UIButtonTypeCustom,图像将是蓝色的iOS7和8
Apfelsaft

2
对于Swift 3:btnTwo.setImage(btnImage,用于:UIControlState.normal)
·蒙托亚

57

Mike的解决方案将仅显示图像,但是在按钮上设置的任何标题将不可见,因为您可以设置标题或图像。

如果要同时设置(您的图像和标题),请使用以下代码:

btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];

2
setImagesetBackgroundImage有什么区别?
onmyway133

3
setBackgroundImage将在标题文本上的按钮宽度上拉伸图像,而setImage将使按钮忽略标题文本而无需拉伸图像。
欧内斯特(Ernest)

21

在此之前,我必须根据图像帧的大小显式调整按钮帧的大小。

UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];

// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;

[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController 
               action:@selector(revealToggle:) 
     forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = 
  [[UIBarButtonItem alloc] initWithCustomView:listButton];

self.navigationItem.leftBarButtonItem = jobsButton;

@jrasmusson只是想补充一点,也可以在框架中摆弄三行文字[listButton sizeToFit]
bk138 '16

10

如果是Swift用户

// case of normal image
let image1 = UIImage(named: "your_image_file_name_without_extension")!
button1.setImage(image1, forState: UIControlState.Normal) 

// in case you don't want image to change when "clicked", you can leave code below
// case of when button is clicked
let image2 = UIImage(named: "image_clicked")!
button1.setImage(image2, forState: UIControlState.Highlight) 

9

你可以这样

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];

9
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 150, 44);
[btn setBackgroundImage:[UIImage imageNamed:@"buttonimage.png"] 
               forState:UIControlStateNormal];
[btn addTarget:self 
     action:@selector(btnSendComment_pressed:) 
     forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];

8

您可以采用以下两种方式之一放置图像:

UIButton *btnTwo = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
btnTwo.frame = CGRectMake(40, 140, 240, 30);
[btnTwo setTitle:@"vc2:v1" forState:UIControlStateNormal];
[btnTwo addTarget:self 
           action:@selector(goToOne) 
 forControlEvents:UIControlEventTouchUpInside];


[btnTwo setImage:[UIImage imageNamed:@"name.png"] forState:UIControlStateNormal];

//OR setting as background image

[btnTwo setBackgroundImage:[UIImage imageNamed:@"name.png"] 
                  forState:UIControlStateNormal];

[self.view addSubview:btnTwo];

这部分代码使我感到困惑。<code> btnImage = [UIImage imageNamed:@“ image.png”]; </ code>什么是btnImage?原始代码中没有。这是一个新按钮吗?
Mike Martin,

您可以直接设置按钮的图像,而不用像这样获取另一个图像对象:[btnTwo setImage:[UIImage imageNamed:@“ image.png”]];
Ajay Sharma,

您必须将buttonWithType:UIButtonTypeRoundedRect转换为UIButtonTypeCustom,否则其他智能按钮将不会按照您的图像显示。
Praveen-K

2

我一直在寻找一个解决的添加UIImage到我的UIButton。问题在于它显示的图像超出了所需。刚刚帮助了我:

_imageViewBackground = [[UIImageView alloc] initWithFrame:rectImageView];
_imageViewBackground.image = [UIImage imageNamed:@"gradientBackgroundPlain"];
[self addSubview:_imageViewBackground];
[self insertSubview:_imageViewBackground belowSubview:self.label];
_imageViewBackground.hidden = YES;

每次我要显示我时,UIImageView我都将var设置hiddenYESNO。可能还有其他解决方案,但是我对此东西感到很困惑,并解决了它,因此我不需要处理内部事务UIButton是在后台执行的。


2

不必太担心从代码中构筑按钮,您可以在情节提要上进行操作。这对我有用,一行...更简单。

[self.button setBackgroundImage:[UIImage imageNamed: @"yourPic.png"] forState:UIControlStateNormal];

2
-(void)buttonTouched:(id)sender
{
    UIButton *btn = (UIButton *)sender;

    if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"icon-Locked.png"]])
    {
        [btn setImage:[UIImage imageNamed:@"icon-Unlocked.png"] forState:UIControlStateNormal];
        // other statements....
    }
    else
    {
        [btn setImage:[UIImage imageNamed:@"icon-Locked.png"] forState:UIControlStateNormal];
        // other statements....
    }
}

2

Swift 3版本(butt_img必须是Xcode中的Assets.xcassetsImages.xcassets文件夹中的图像集):

btnTwo.setBackgroundImage(UIImage(named: "butt_img"), for: .normal)
btnTwo.setTitle("My title", for: .normal)

无论如何,如果要缩放图像以填充按钮的大小,可以UIImageView在其上添加一个并为其分配图像:

let img = UIImageView()
img.frame = btnTwo.frame
img.contentMode = .scaleAspectFill
img.clipsToBounds = true
img.image = UIImage(named: "butt_img")
btnTwo.addSubview(img)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.