简体   繁体   中英

How to create a custom icon for a UIBarButton?

Swift

I want to create a custom icon downloaded from the internet for a 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

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. 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.

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:

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

If you need any clarification of what I've said, feel free to ask.

Loic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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