在代码中为 UIButton 设置图像 [英] Setting an image for a UIButton in code

查看:34
本文介绍了在代码中为 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];

斯威夫特 5.1

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

这篇关于在代码中为 UIButton 设置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆