Search code examples
swiftswiftuisvgkitmacaw

Macaw SVG Image


So I am trying to get an SVG image to show in SwiftUI however the documentation to the plugin Macaw is not written well - according to the GitHub.

So how can I the following code to work?

First I import this after pod install:

import Macaw

then I add this:

SVGImage(svgName: "location")
                                       .frame(width: 550, height: 550)

However I get:

Cannot find 'SVGImage' in scope

I have also installed SVGKit, I am trying to get it to work with that.


Solution

  • Look like you took the first part of this SO answer

    But forgot to implement SVGImage struct

    import SwiftUI
    import Macaw
    
    struct SVGImage: UIViewRepresentable {
    
        var svgName: String
    
        func makeUIView(context: Context) -> SVGView {
            let svgView = SVGView()
            svgView.backgroundColor = UIColor(white: 1.0, alpha: 0.0)   // otherwise the background is black
            svgView.fileName = self.svgName
            svgView.contentMode = .scaleToFill
            return svgView
        }
    
        func updateUIView(_ uiView: SVGView, context: Context) {
    
        }
    
    }