Apple Reference Image: How It Works
In this article
Apple Reference Image is a feature in Xcode that lets you define a stable, named color value that can be referenced across your app’s assets, so you can change the actual color in one place without editing code. You create a reference image (a small PNG file) that acts as a key, then map that key to a specific color in an asset catalog; when you use the reference image in your code or storyboard, Apple’s tools resolve it to the mapped color at build time. This article explains what reference images are, how to create and use them, and common pitfalls to avoid.
Background: Why Apple Added Reference Images
Before Xcode 11, developers who wanted a consistent color palette had to either hardcode RGB values in code or create multiple color sets and manually keep them in sync. Apple introduced the Color Set asset type in Xcode 9, but that still required you to drag the same color into every target that needed it. Reference images solve a different problem: they let you define a semantic name (like “PrimaryButton”) that can be swapped per appearance (light/dark mode) or per target, without touching code. The reference image itself is never displayed; it only serves as a lookup key. Apple’s documentation describes it as a way to “reference a color by name” across asset catalogs, making it easier to share a palette between an app and its extensions or watchOS companion.
How Apple Reference Image Works
In an asset catalog, a reference image is a special entry that contains a name and a pointer to a color. When you create one, Xcode generates a small PNG file (usually 1x1 pixel) that stores the name as metadata. You then assign that reference image to a color set’s “Universal” or “Any Appearance” slot. When your app runs, the system reads the reference image’s name, looks up the corresponding color in the same catalog, and uses that color wherever the reference image was used. This indirection means you can change the actual color in the color set, and every place that uses the reference image updates automatically.
For example, you might have a color set named “Accent” with a light‑mode value of #007AFF and a dark‑mode value of #0A84FF. You create a reference image named “AccentRef” and set its “Color” property to the “Accent” color set. In your storyboard, you set a button’s background to “AccentRef”. At runtime, the button shows the correct color for the current appearance. If you later change “Accent” to a different blue, the button updates without recompiling the storyboard.
How to Create and Use a Reference Image
Creating a reference image takes about a minute. Follow these steps in Xcode (version 12 or later, though the feature works in 11 as well):
- Open your asset catalog (Assets.xcassets) in Xcode.
- Click the “+” button in the bottom‑left corner and choose “New Reference Image”.
- Name it something descriptive, like “PrimaryButtonColor”.
- In the Attributes inspector, you’ll see a “Color” field. Click the dropdown and select the color set you want to reference (e.g., “Accent”). If you haven’t created that color set yet, do that first.
- Use the reference image anywhere you would use a color: in storyboards, SwiftUI’s
Color("PrimaryButtonColor"), or UIKit’sUIColor(named: "PrimaryButtonColor").
Note that the reference image file itself is just a placeholder; it doesn’t contain any pixel data that matters. You can even set its size to 1x1 in the attributes. The important part is the “Color” assignment.
Choosing Between Reference Images and Color Sets
You might wonder why you wouldn’t just use a color set directly. The answer is that reference images are useful when you need to share a color across multiple asset catalogs. For instance, if you have an app and a watchOS extension, each has its own asset catalog. If you define a color set in the app’s catalog, the watch extension can’t see it. But you can put a reference image in the watch extension’s catalog that points to the same color set name in the app’s catalog. This way, you only define the actual color once, and both targets reference it. Reference images are also handy when you want to use the same semantic name in a storyboard that is shared between targets, but the actual color differs per target (e.g., a white label on a dark background for one app, black on light for another).
In short, use a color set when you only need the color in one catalog; use a reference image when you need to reference a color that lives in another catalog or when you want to decouple the name from the value across targets.
Common Problems and How to Fix Them
Several issues can trip up developers new to reference images:
- Color not updating: If you change the color set but the reference image still shows the old color, make sure you’ve assigned the reference image’s “Color” property to the color set, not just left it as “Default”. Also, clean the build folder (Product > Clean Build Folder) to clear the asset cache.
- Reference image not found in code: The name you use in code must exactly match the reference image’s name, including case. For SwiftUI, use
Color("Name"); for UIKit,UIColor(named: "Name"). If you get nil, check that the reference image is in the correct asset catalog and that the target membership includes it. - Dark mode not working: Reference images inherit the appearance variants of the color set they point to. If your color set has only a “Universal” color, it won’t adapt to dark mode. Add “Any Appearance” and “Dark” slots to the color set, not to the reference image.
- Reference image appears as a blank white square: That’s normal in the asset catalog; the image is just a placeholder. It won’t appear in the app.
Comparison: Reference Image vs. Named Colors in Code
Before reference images, developers often used a central constants file with RGB values. That approach works but has drawbacks: you must manually update every target, and you can’t easily adapt to appearance changes without writing conditional code. Reference images move the color definition into the asset catalog, where Xcode can automatically handle light/dark mode and high contrast variants. The main trade‑off is discoverability: a developer new to the project might not realize that a color is defined via a reference image, so it’s good practice to name reference images clearly and add a comment in code if needed.
Another alternative is to use system colors like .systemBlue, but those are fixed and don’t match a custom brand palette. Reference images give you the flexibility of custom colors with the convenience of a named reference.
Costs and Limitations
There is no monetary cost to using reference images; they are a free feature of Xcode. The main “cost” is in project complexity. Each reference image adds a small file to your asset catalog, and if you have many colors, the catalog can become cluttered. Also, reference images only work within Apple’s ecosystem—they have no effect on web or Android versions of your app. If you need cross‑platform consistency, you’ll still need a separate color management system.
Frequently Asked Questions
Can a reference image reference another reference image?
No. A reference image must point to a color set, not to another reference image. If you try to set the “Color” field to a reference image, Xcode will not allow it. You can, however, have multiple reference images point to the same color set.
Do reference images work in SwiftUI?
Yes. In SwiftUI, you use Color("ReferenceName") exactly as you would with a color set. The system resolves the reference image to the underlying color at runtime. This works on iOS 13+, macOS 10.15+, watchOS 6+, and tvOS 13+.
Can I use a reference image for a gradient or pattern?
No. Reference images only point to a solid color set. If you need a gradient, you’ll have to use a regular image asset or create a gradient in code. Reference images are strictly for color lookup.
Why does my reference image show as a white square in the asset catalog?
That’s expected. The reference image is a placeholder; it doesn’t contain any visible content. The white square is just how Xcode renders a 1x1 transparent PNG. The actual color is defined in the color set you assigned.
What happens if I delete the color set that a reference image points to?
The reference image will no longer have a valid color, and Xcode will show a warning. At runtime, the color will fall back to the default (usually black or clear, depending on context). To fix it, either recreate the color set with the same name or reassign the reference image to another color set.
Can I use reference images in asset catalogs for app extensions?
Yes, that’s one of the main use cases. Put a reference image in the extension’s asset catalog and point it to a color set that exists in the containing app’s catalog. The extension will resolve the color at runtime as long as the containing app is installed.