exyte / Macaw

Powerful and easy-to-use vector graphics Swift library with SVG support
MIT License
6k stars 552 forks source link

Issue with SVGImage and SwiftUI #751

Open GDGapps opened 3 years ago

GDGapps commented 3 years ago

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.

argiad commented 3 years ago

Same issue Need help

Screen Shot 2021-04-18 at 8 35 49 PM
gunhansancar commented 2 years ago

@GDGapps any chance you found a solution?