0

I'm building a modular iOS app with SwiftUI views in a framework target (MyFrameworkTarget). I added a new .xcassets catalog to this target and defined some named colors like "durationBackground". The assets work at runtime, but in SwiftUI previews, the colors fail to load (they render as clear/black).

I tried the following-

  • Ensured .xcassets is included in the correct framework target (Target Membership and Copy Bundle Resources).

  • Accessed the colors using:

    Color("durationBackground", bundle: Bundle(for: SomeClass.self))

Color("durationBackground", bundle: Bundle(identifier: "com.mycompany.MyFrameworkTarget"))

What is the correct way to load colors from an .xcassets catalog inside a SwiftUI framework target, so that SwiftUI previews can render them correctly?

1 Answer 1

0

if your Package is Internal:

you should define your asset such as below structure:

enter image description here

and also in your "Package.swift" file should define your resource such as:

`

import PackageDescription

let package = Package(
    name: "YourPackageName",
    platforms: [
         .iOS(.v13)
    ],
    products: [
        .library(
            name: "YourPackageName",
            targets: ["YourPackageName"]),
    ],
    targets: [
        .target(
            name: "YourPackageName",
            resources: [.process("Resources")]
        ),
    ]
)

this is required:

resources: [.process("Resources")]

if your Package is External:

The problem maybe is for the bundle definition
There are two solutions for the bundle definition if your package is external(it depends on your package manager pod or spm):

var bundle = Bundle.module
#if SWIFT_PACKAGE
        bundle = Bundle.module
#else
        bundle = Bundle(for: self)
#endif

Check this link:
How to define Bundle as internal For Pod Lib and SPM that handles both of them For using package images

Sign up to request clarification or add additional context in comments.

2 Comments

Where do you put that code?
the problem is for the bundle definition in the link I explain the bundle definition? got it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.