Macaw icon indicating copy to clipboard operation
Macaw copied to clipboard

Issue with SVGImage and SwiftUI

Open GDGapps opened this issue 4 years ago • 2 comments

Hello,

if I use SwiftUI, the SVGImage is either blank or not correctly scaled (depending on each SVG file). The same files work correctly on the Storyboard.

This is my code:

import SwiftUI
import Macaw

struct CountryRow: View {
    var body: some View {
        Text("Task data goes here")
    }
}

struct MapRow: View {
    var body: some View {
        HStack {
            SVGImage(name: "albaniaLow")
                .frame(width: 50, height: 50)
            Text("hello")
            Spacer()
        }
    }
}

struct SVGImage: UIViewRepresentable {
    // a binding allows for dynamic updates to the shown image
    @Binding var name: String

    init(name: Binding<String>) {
        _name = name
    }

    // convenience constructor to allow for a constant image name
    init(name: String) {
        _name = .constant(name)
    }

    func makeUIView(context: Context) -> SVGView {
        
        let svgView = SVGView()
        
        svgView.backgroundColor = UIColor(white: 1.0, alpha: 0.0) // otherwise the background is black
        svgView.contentMode = .scaleAspectFit
        return svgView
    }

    func updateUIView(_ uiView: SVGView, context: Context) {
        
        uiView.fileName = name
    }
}


struct CountriesUIView: View {
    var body: some View {
        
        NavigationView {
            List {
                MapRow()
                Section(header: Text("Continent 1")) {
                    CountryRow()

                }
                Section(header: Text("Continent 2")) {
                    CountryRow()
                }
            }.navigationBarTitle("Navigation")
        }
    }
}

struct CountriesUIView_Previews: PreviewProvider {
    static var previews: some View {
        CountriesUIView()
    }
}

Am I doing something wrong? Thank you in advance.

GDGapps avatar Mar 01 '21 15:03 GDGapps

Same issue Need help Screen Shot 2021-04-18 at 8 35 49 PM

argiad avatar Apr 19 '21 01:04 argiad

@GDGapps any chance you found a solution?

gunhansancar avatar Feb 23 '22 18:02 gunhansancar