Differentiating images in an asset catalog by their group
Asked Answered
S

2

14

Is there any way to use asset catalog groups to effectively namespace multiple images with a shared name?

For example, if I have multiple images named Foo that are in different groups, is there a way to specify which one I would like with imageNamed: (i.e. something like UIImage(named:"Group1.Foo")).

Obviously I could create unique names myself for all images, but it would be nice if there was a way to leverage groups to do it for me.

Swound answered 21/1, 2015 at 20:55 Comment(0)
H
13

Select each folder in your asset catalog that you'd like to add a namespace for. Go to the folder attributes and select under the name "Provides Namespace". This should properly set sub-directories for your final asset catalog.

You can do this manually by editing each folder's Contents.json. You would add the following with proper formatting.

"properties" : {
      "provides-namespace" : true
}
Headstone answered 29/9, 2015 at 21:44 Comment(0)
D
14

Visual Instructions

(based on Xcode 10.1)

1) Select the asset catalog folder.

enter image description here

*The folder is yellow if "Provides Namespace" is false.

2) Make sure the Inspectors panel is visible

enter image description here

3) In the Inspectors panel, select the Attributes Inspector. Check the "Provides Namespace" checkbox.

enter image description here

Notice that the folder icon turns blue indicating that "Provides Namespace" is true.

enter image description here

When referencing the image in code, remember to include the namespace.

let image = UIImage(named: "Icon/Menu")
Domitiladomonic answered 18/2, 2019 at 4:14 Comment(1)
How to included the namespace in code was what I was looking for. Thank you for including that to make the answer complete.Commercialism
H
13

Select each folder in your asset catalog that you'd like to add a namespace for. Go to the folder attributes and select under the name "Provides Namespace". This should properly set sub-directories for your final asset catalog.

You can do this manually by editing each folder's Contents.json. You would add the following with proper formatting.

"properties" : {
      "provides-namespace" : true
}
Headstone answered 29/9, 2015 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.