How to use custom image in SwiftUI Toggle

Viewed 1210

I am trying to make a toggle button with a custom image, and currently I can use a SF Symbol, but not a custom image. Here is the ToggleStyle code:

struct CheckboxToggleStyle: ToggleStyle {
    func makeBody(configuration: Configuration) -> some View {
        return HStack {
            configuration.label
            Spacer()
            Image(systemName: configuration.isOn ? "checkmark.circle.fill" : "circle.fill")
                .resizable()
                .frame(width: 50.0, height: 50.0)
                .onTapGesture { configuration.isOn.toggle() }
        }.frame(width: 50.0, height: 50.0)
    }
}

This works when I use the "checkmark" and "circle" symbols, but when I switch it to an image from my assets folder I get an the error:

2020-07-27 16:46:06.476638-0500 myProgram[4587:1288628] [SwiftUI] No image named 'MY IMAGE HERE' found in asset catalog for main bundle

1 Answers

The initialiser you're using is for SF Symbols only. It loads system images.

You can replace it:

Image(systemName: "checkmark.circle.fill")

with this:

Image("myCustomImageFromAssets")
Related