在 XCode 5 中使用多个资产目录的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19495850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 04:08:32  来源:igfitidea点击:

Issue with using multiple asset catalogs in XCode 5

iosxcodeios7xcode5

提问by Mark Horgan

I have one app that has 2 variations that only differ in branding. For each version I have an AppIcon and LaunchImage in an asset catalog. I've created a different target for each version. In the second version when I try and select AppIcon in the second asset catalog it just defaults to the first one. Removing the first asset catalog seems to resolve it but I would prefer a less 'hacky' solution.

我有一个应用程序,它有 2 个变体,仅在品牌上有所不同。对于每个版本,我在资产目录中都有一个 AppIcon 和 LaunchImage。我为每个版本创建了不同的目标。在第二个版本中,当我尝试在第二个资产目录中选择 AppIcon 时,它只是默认为第一个。删除第一个资产目录似乎可以解决它,但我更喜欢一个不那么“hacky”的解决方案。

回答by Mark Horgan

I think the best solution I've come up with so far is to rename assets in subsequent asset catalogs. For example you might have 2 asset catalogs that contain assets named AppIcon and LaunchImage. In the 2nd asset catalog I've renamed AppIcon and LaunchImage to AppIcon1 and LaunchImage1. So now I can select them in the 'General' section of the target's build settings.

我认为迄今为止我提出的最佳解决方案是重命名后续资产目录中的资产。例如,您可能有 2 个资产目录,其中包含名为 AppIcon 和 LaunchImage 的资产。在第二个资产目录中,我将 AppIcon 和 LaunchImage 重命名为 AppIcon1 和 LaunchImage1。所以现在我可以在目标构建设置的“常规”部分中选择它们。

回答by Idan

  1. Use the same asset catalog for both targets.
  2. Check that it is included on both targets (check target membership using file inspector).
  3. On each target select the appropriate name of asset (icons).
  1. 对两个目标使用相同的资产目录。
  2. 检查它是否包含在两个目标中(使用文件检查器检查目标成员资格)。
  3. 在每个目标上选择适当的资产名称(图标)。

This shall work, I'm using it with 8 targets so...

这应该有效,我将它与 8 个目标一起使用,所以......

Btw, I discover that sometimes launch-image isn't working when using few targets so you may need to duplicate then select it (Xcode bug).

顺便说一句,我发现有时使用少量目标时启动图像不起作用,因此您可能需要复制然后选择它(Xcode 错误)。

回答by d.ennis

In my case I am using cocoa pods. The cocoa pods script Pods-resources.shsomehow caused the problem. removing the asset parts from the script solved the problem.

就我而言,我使用的是可可豆荚。可可豆脚本Pods-resources.sh以某种方式导致了这个问题。从脚本中删除资产部分解决了这个问题。

Make sure to clean your project and delete derived data folder.

确保清理您的项目并删除派生数据文件夹。

Parts to be deleted can be found here:

要删除的部分可以在这里找到:

https://stackoverflow.com/a/21309102/832111

https://stackoverflow.com/a/21309102/832111

回答by Priebe

I'm running with several targets where each has their own asset catalog file, with both AppIcon, LaunchImage and a custom I use in storyboard for backgrounds.

我正在运行多个目标,每个目标都有自己的资产目录文件,包括 AppIcon、LaunchImage 和我在故事板中用于背景的自定义。

It sounds like the same problem I had, when selecting the asset catalog for App Icons and Launch Images it took the wrong one when building. I solved it by modifying the "Target Membership" for each assets catalog, the .xcassets file. It's set so each xcassets file only have the target it's related to. Thereby only one target is selected per asset catalog. Now it's the right AppIcon and LaunchImage that's available for each target.

这听起来与我遇到的问题相同,当为 App Icons 和 Launch Images 选择资产目录时,它在构建时出错了。我通过修改每个资产目录的“目标成员资格”(.xcassets 文件)解决了这个问题。它的设置使每个 xcassets 文件只有与其相关的目标。因此,每个资产目录仅选择一个目标。现在是每个目标可用的正确 AppIcon 和 LaunchImage。

回答by user216661

After much frustration, I have clean, successful, separate images assets. Here's what worked for me:

在经历了很多挫折之后,我有了干净、成功、独立的图像资产。这是对我有用的:

  1. I used XCode to create an image catalog for the first target (File, New, File, Asset). It has a membership for that target only. As Mark Horgan suggested, I named the internal appIcon & launchImages, if only to make it easier for myself.

  2. Next, I deleted that catalog from my project using the option to remove references.

  3. I repeated step 1 and created an image catalog for the 2nd target. It has a membership for that 2nd target only. Assigning the catalog to my target was tricky because XCode only offered "Don't Use Image Catalog" as an option. I choose that option, and then XCode offers "Use Asset Catalog". I chose my new catagot. Again I named the internal appIcon & launchImages to reference the 2nd target, so I could recognize it.

  4. As in 2, I removed the catalog with a remove references delete.

  5. Departing XCode for a moment, I opened a Finder window and located my two different images.xcassets files which had been put in different directories by XCode. I renamed those files to each reference their target. (I also moved them into the master folder, but of course that's a preference.)

  6. Back in XCode, I (re)added the xcassets files, one at a time, ensuring they were appropriately added to their target and copied to resources.

  7. Then in XCode I selected each target and used the General tab to properly specify the image catalog for each target.

  1. 我使用 XCode 为第一个目标(文件、新建、文件、资产)创建了一个图像目录。它仅具有该目标的成员资格。正如 Mark Horgan 所建议的,我将内部 appIcon 和 launchImages 命名为,只是为了让我自己更轻松。

  2. 接下来,我使用删除引用的选项从我的项目中删除了该目录。

  3. 我重复了步骤 1 并为第二个目标创建了一个图像目录。它仅具有该第二个目标的成员资格。将目录分配给我的目标很棘手,因为 XCode 仅提供“不要使用图像目录”作为选项。我选择了那个选项,然后 XCode 提供了“使用资产目录”。我选择了我的新目录。我再次将内部 appIcon 和 launchImages 命名为引用第二个目标,因此我可以识别它。

  4. 在 2 中,我使用删除引用删除删除了目录。

  5. 离开 XCode 片刻,我打开了一个 Finder 窗口,找到了我的两个不同的 images.xcassets 文件,它们被 XCode 放在了不同的目录中。我将这些文件重命名为每个引用它们的目标。(我也将它们移动到主文件夹中,但当然这是一个偏好。)

  6. 回到 XCode,我(重新)添加 xcassets 文件,一次一个,确保它们被适当地添加到目标并复制到资源。

  7. 然后在 XCode 中,我选择了每个目标并使用“常规”选项卡为每个目标正确指定图像目录。

回答by Wladek Surala

I used @user216661 solution, but in my case I didn't have to do steps 2-7.

我使用了@user216661 解决方案,但就我而言,我不必执行步骤 2-7。

I created an asset folder with specific name for specific target (in my case, one asset folder per target). Then I added the images to those xcassets folders by dragging & dropping them from Finder. The same for icons and launch images. I didn't have to customize graphics names for each target.

我为特定目标创建了一个具有特定名称的资产文件夹(在我的情况下,每个目标一个资产文件夹)。然后我通过从 Finder 中拖放图像将图像添加到这些 xcassets 文件夹中。图标和启动图像也是如此。我不必为每个目标自定义图形名称。

In this solution, you don't store any graphics outside asset folders.

在此解决方案中,您不会在资产文件夹外存储任何图形。

回答by frank

How about just modify the assets in build setting option.For me,it worked.

只修改构建设置选项中的资产怎么样。对我来说,它有效。