如何在代码中为 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];
但看不到什么会为它设置图像。
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)