2

我的源代码非常简单,如下所示:

//
//  SVGImage.swift
//  WorldRoad
//
//  Created by norains on 2019/11/26.
//  Copyright © 2019 norains. All rights reserved.
//

import PocketSVG
import SwiftUI

struct SVGImage: UIViewRepresentable {

    func makeUIView(context: Context) -> SVGImageView {
        let url = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
        let svgImageView = SVGImageView.init(contentsOf: url)
        //svgImageView.frame = view.bounds //Could not set the frame here
        svgImageView.contentMode = .scaleAspectFit
        
        return svgImageView
    }

    func updateUIView(_ view: SVGImageView, context: Context) {
        
    }
}

struct SVGImage_Previews: PreviewProvider {
    static var previews: some View {
        return SVGImage()
    }
}

但它显示就像这样: 在此处输入图像描述

看起来视图比屏幕大。

正常应该是这样的: 在此处输入图像描述

我该怎么办?谢谢!

4

1 回答 1

2

SVGImageView如果您将内部嵌入如下内容,它将起作用UIView

import PocketSVG
import SwiftUI

struct SVGImage: UIViewRepresentable {
    func makeUIView(context: Context) -> UIView {
        let svgView = UIView(frame: UIScreen.main.bounds)
        let url = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
        let svgImageView = SVGImageView.init(contentsOf: url)
        svgImageView.frame = svgView.bounds
        svgImageView.contentMode = .scaleAspectFit
        svgView.addSubview(svgImageView)

        return svgView
    }

    func updateUIView(_ view: UIView, context: Context) {

    }
}

struct SVGImage_Previews: PreviewProvider {
    static var previews: some View {
        return SVGImage()
    }
}
于 2019-11-27T03:19:37.597 回答