Search code examples
xcodeassetstargetworkspace

Specify which .xcassets catalogue to use an image from in XIB


If I had:

  • two targets (A and B) in one Xcode project
  • two different asset catalogues (a.xcassets and b.xcassets)
    • a.xcassets assigned to target A only
    • b.xcassets assigned to target B only
  • 1.png called Image in a.xcassets and 2.png also called Image in b.xcassets

When building:

  • 1.png would end up in Assets.car in A.app
  • 2.png would end up in Assets.car in B.app

Both images could be referred to as Image in code or IB files with the correct image showing up in the respective build App.

So far, so good. Now my problem:

When adding a UIImageView to a b.xib belonging only to target B and referring to Image in that UIImageView, it doesn’t actually show 2.png in the XIB display in Xcode but rather 1.png. Xcode should know that b.xib belongs to target B and that it should look out for Image in b.xcassets rather than a.xcassets (as a.xcassets has no membership with target B).

What can I do to design my layout with the correct Image (2.png)?

Is there a way to tell Xcode a namespace or specify the asset catalogue to take Image from? I tried b/Image and b.xcassets/Image.

PS: the same goes for two projects (with a single target each) in one workspace


Solution

  • Basically, don’t do that. Don’t give the same name to image sets in different asset catalogs. As you have shown, it confuses Interface Builder.

    You might be able to differentiate using asset catalog “folder” feature for name spacing but I have not found that to be totally reliable either.