在代码中为UIButton设置图像 [英] Setting an image for a UIButton in code
本文介绍了在代码中为UIButton设置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在代码中为UIButton设置图像?
How do you set the image for a UIButton in code?
我有:
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];
但是看不到会为它设置图像的内容。
but don't see what will set the image for it.
推荐答案
Objective-C
Objective-C
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
Swift 4
let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)
这篇关于在代码中为UIButton设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文