首页 > 解决方案 > 想要将 UIbutton 更改为我选择的图像

问题描述

UIButton *openButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
openButton.frame = CGRectMake((main.frame.size.width/2)-15, (main.frame.size.height/2)+75, 30, 30);
openButton.backgroundColor = [UIColor clearColor];
openButton.layer.cornerRadius = 16;
openButton.layer.borderWidth = 2;
openButton.layer.borderColor = themeColor.CGColor;
[openButton addTarget:self action:@selector(wasDragged:withEvent:) 
forControlEvents:UIControlEventTouchDragInside];
[openButton addTarget:self action:@selector(showMenu) 
forControlEvents:UIControlEventTouchDownRepeat]; 

这是我的打开按钮的代码,它已经在 mod 菜单中实现了,但我真的想将其更改为示例图像而不是圆形。我尝试将其更改为 UIImage,但由于我是编码新手,因此我不确定如何正确执行。

标签: xcode

解决方案


请试试这个:

yourBtn.setImage(UIImage(named: "ImgName"), for: .normal)

请转换为目标c。setImage是 UIButton 的一个属性,用于图像设置。

它可能会帮助你。谢谢


推荐阅读