在代码中为UIButton设置图像

Spa*_*nky 194 iphone objective-c uibutton ipad ios

如何在代码中设置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];
Run Code Online (Sandbox Code Playgroud)

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

小智 390

Objective-C的

UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

斯威夫特4

let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)
Run Code Online (Sandbox Code Playgroud)

  • 注意:如果您使用除UIButtonTypeCustom以外的任何内容,则iOS7和8中的图像将为蓝色. (12认同)
  • 对于Swift 3:btnTwo.setImage(btnImage,for:UIControlState.normal) (2认同)

lev*_*han 57

Mike的解决方案只显示图像,但按钮上的任何标题都不可见,因为您可以设置标题或图像.

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

btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

  • setBackgroundImage将在按钮宽度上拉伸图像覆盖标题文本,而setImage将使按钮成为忽略标题文本而不拉伸图像的图像. (3认同)
  • **setImage**和**setBackgroundImage**有什么区别? (2认同)

小智 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;
Run Code Online (Sandbox Code Playgroud)


Sru*_*Suk 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) 
Run Code Online (Sandbox Code Playgroud)


Azh*_*har 9

你可以这样做

[btnTwo setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)


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


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