简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM