Search code examples
swiftxcodeassets

Get color from custom Assets.xcassets file


I have a separate DesignSystem package in my project. In that package, I created new Assets.xcassets and added some colours with appropriate names there. Now I want to reach that colours from the same package using the name: I am trying:

let name = "myImageName"
let color = Color(name)
let uiColor = UIColor(named: name)

But none of them works.

My Idea is that, since I am using a separate Package and I have created a new custom Assets.xcassets then I have to specify some path in the name of configuring something.

The same problem exists with other assets, like Image.

Using XCode: 14.3


Solution

  • Since resources are in the module and not in the main bundle, you have to write it explicitly:

    Color(name, bundle: .module)