Differentiating images in an asset catalog by their group

北城以北 提交于 2019-12-06 23:52:04

问题


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.


回答1:


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
}



回答2:


Visual Instructions

(based on Xcode 10.1)

1) Select the asset catalog folder.

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

2) Make sure the Inspectors panel is visible



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



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



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

let image = UIImage(named: "Icon/Menu")


来源:https://stackoverflow.com/questions/28076644/differentiating-images-in-an-asset-catalog-by-their-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!