3 回答
TA贡献1772条经验 获得超5个赞
目标C
UIImage *btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setImage:btnImage forState:UIControlStateNormal];
斯威夫特4
let btnImage = UIImage(named: "image")
btnTwo.setImage(btnImage , for: UIControlState.normal)
TA贡献1776条经验 获得超12个赞
Mike的解决方案将仅显示图像,但是在按钮上设置的任何标题将不可见,因为您可以设置标题或图像。
如果要同时设置(您的图像和标题),请使用以下代码:
btnImage = [UIImage imageNamed:@"image.png"];
[btnTwo setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnTwo setTitle:@"Title" forState:UIControlStateNormal];
TA贡献1810条经验 获得超4个赞
在此之前,我必须根据图像帧的大小显式调整按钮帧的大小。
UIImage *listImage = [UIImage imageNamed:@"list_icon.png"];
UIButton *listButton = [UIButton buttonWithType:UIButtonTypeCustom];
// get the image size and apply it to the button frame
CGRect listButtonFrame = listButton.frame;
listButtonFrame.size = listImage.size;
listButton.frame = listButtonFrame;
[listButton setImage:listImage forState:UIControlStateNormal];
[listButton addTarget:self.navigationController.parentViewController
action:@selector(revealToggle:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton =
[[UIBarButtonItem alloc] initWithCustomView:listButton];
self.navigationItem.leftBarButtonItem = jobsButton;
- 3 回答
- 0 关注
- 705 浏览
添加回答
举报