简体   繁体   English

如何为UIBarButton创建自定义图标?

[英]How to create a custom icon for a UIBarButton?

Swift 迅速

I want to create a custom icon downloaded from the internet for a UIBarButton . 我想创建一个从Internet下载的UIBarButton的自定义图标。 Do I have to do this programmatically or can I some how find a setting via storyboard? 我必须以编程方式执行此操作,还是可以通过情节提要找到一些设置? If I choose " custom " it doesn't lead to further choosing of custom icons or anything so I'm not really sure what to do here. 如果我选择“ 自定义 ”,则不会导致进一步选择自定义图标或其他任何东西,因此我不确定在这里做什么。

I see there is a " Bar Item: Image " section. 我看到有一个“ 栏项目:图像 ”部分。 Is this what I will use? 这是我要用的吗?

http://prntscr.com/5x7gn0 http://prntscr.com/5x7gn0

Thanks! 谢谢!

Yes, the image (at the end of that link you posted) is where you would select your image. 是的,图像(在您发布的链接的末尾)是您选择图像的位置。 You need to put it into your project first though. 不过,您需要先将其放入项目中。

So steps to do: 所以要做的步骤:

  1. Drag the image (hopefully it's in PNG format?) into the sidebar in the left and tick the checkbox titled "Copy items if needed" and click finish. 将图像(希望是PNG格式?)拖到左侧的侧边栏中,并选中标题为“如果需要,复制项目”的复选框,然后单击完成。 Now you should see the image in your project. 现在您应该在项目中看到该图像。
  2. Go to that place in interface builder that you have in the link (here: http://prntscr.com/5x7gn0 ) and select the image you added from the drop down list. 转到接口构建器中该链接中的位置(此处: http : //prntscr.com/5x7gn0 ),然后从下拉列表中选择添加的图像。

There is another way you can do this; 您还有另一种方法可以执行此操作; programmatically, but it's slightly trickier. 以编程方式进行,但是有点棘手。 You still do step 1 but then instead of step 2, you can create an outlet to the Bar Button item and set its image like this: 您仍然执行第1步,但可以代替第2步,为Bar Button项创建一个插座,并按如下所示设置其图像:

barButtonItem.image = UIImage(named: "imageName.PNG")

If you need any clarification of what I've said, feel free to ask. 如果您需要我所说的话,请随时提出。

Loic 卢瓦克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM