如何在代码中为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];

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

最佳答案

物镜

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

斯威夫特5.1
let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: .normal)

10-08 06:24