Guig
Guig

Reputation: 10169

Image not showing in SwiftUI preview when using Swift Package Manager - works fine in Xcode project

I'm wondering if anyone got SwiftUI preview to work with a local image asset when using Swift Package Manager.

I'm not doing anything fancy. I have an asset catalog with a few images and my view looks like

public var body: some View {
    Image("star_5")
        .resizable()
        .scaledToFit()
}

When I create an Xcode project that depends on this swift package, I can reference the previews defined there and SwiftUI shows them correctly.

Upvotes: 8

Views: 5091

Answers (1)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119118

When you are inside a package, you need to pass in the bundle, because it is the main by default and there is no main in a module.

So you need to use .module as the bundle:

Image("star_5", bundle: .module)

.module is an auto-generated resource. You don't need to manually write it!


Note

If you faced this error:

Type 'Bundle' has no member 'module'

Follow this instruction to make it automatically build for your package

Upvotes: 16

Related Questions