小编典典

在代码中为 UIButton 设置图像

all

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

但看不到什么会为它设置图像。


阅读 133

收藏
2022-06-24

共1个答案

小编典典

Objective-C

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

斯威夫特 5.1

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