Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add an icon to my Xcode project?

Tags:

xcode

macos

icons

I have an Xcode project for Mac OSX, and I'd like to add an icon file (.icns) to my project and set it as the App Icon. How would I do this?

like image 796
Josias Viskoo Avatar asked Jan 15 '13 22:01

Josias Viskoo


1 Answers

The key here is that you need an ICNS (icon set) file.

The right way to create this is described in the Human Interface Guidelines, and the details are covered in various user's guides and reference guides in Apple's maze of twisty little documentation, but I'll summarize it here.

There are third-party tools that can do this, as well as plugins for Photoshop, GIMP, Illustrator, etc., that can output a correct .icns file. But make sure, if you use such a thing, it's up to date, because Apple changes the rules all the time.

If you need to do it manually, here's what you do:

First, create a set of PNG files at different sizes. The exact list of what you need changes over time. See Provide the Correct Resources and Let OS X Do the Work if that link lasts longer than the current list; otherwise, search for it at http://developer.apple.com yourself. But, as of early 2013, it's 512x512, 256x256, 128x128, 32x32, 16x16, and @2x versions of each. All of them should have the sRGB color profile embedded in them. They should be named either [email protected], icon_512x512.png, etc., or [email protected], MyApp_512x512.png, etc. Put them all in a directory together named, e.g., MyApp.iconset.

"But wait! I just want an icon, I don't want all those sizes!" Well, you really do want all those sizes. Your 512x512 icon will look horrible when scaled down to 32x32. And on a Retina Mac, when your icon gets scaled up to double resolution, instead of getting sharper it'll just get jagged. Also, if you want to get into the App Store, Apple will reject you if you don't have them. But, if you insist, you can get away with just putting icon-512x512.png in the folder, and follow the rest of the steps, and it will work.

From the Terminal, cd into the parent directory, and type iconutil -c icns MyApp.iconset. You will get a file called MyApp.icns.

Now you can do the steps suggested by Douglas, and it will actually work. In Xcode, select your project in the Project Navigator, select your app target in the project sidebar, select the Summary tab, and drag MyApp.icns from Finder to the App Icon box.

This may not have any visible effect in the GUI, except to add MyApp.icns to the Project Navigator. In other words, you may still see the "?" icon. This seems to be a bug in Xcode 4.5. If you follow the out-of-date recommendations from the HIG two versions ago, Xcode always shows the icon, but if you follow the current HIG, it doesn't. Go figure. Hopefully Apple will fix that some day.

But for now, it doesn't matter. Build the project, and then look at MyApp.app, and it will have your icon in the Finder, on the Dock, etc.

Now, I know you don't want to draw the same picture in 10 different variations, you just want something simple. As long as you don't want to get into the App Store, you can get away with cheating, in two ways:

  • Scale the 512x512 (1024x1024 pixel "512x512@2x" if supporting hi res) image to all of the other sizes, using your favorite tool.
  • Create a .icns with nothing but the 512x512 image in the iconset.

The second one is simpler, and less cheat-y, and ultimately Finder is probably going to scale your 512x512 image as well as you would have anyway.

Finally, if you've manually edited your Info.plist or changed build settings (or you're using a project imported from a much easier version of Xcode), just dragging the image may not be enough. If you need to do the same steps manually, here they are:

  • MyApp.icns has to be in the Project Navigator as a file in your project. (You can drag it here from Finder.)
  • In Build Phases, the Copy Bundle Resources should include MyApp.icns. (You can drag it here from the Project Navigator.) (If you're not using the normal Build Phases for some reason, you need some other way to get it copied to Contents/Resources/MyApp.icns at build time.)
  • Your Info.plist should have an Icon file (raw name CFBundleIconFile) named MyApp, with no extension.

That's all there is to it.

like image 82
abarnert Avatar answered Oct 08 '22 17:10

abarnert