我的源代码非常简单,如下所示:
//
// 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()
}
}
看起来比屏幕大。
我该怎么办?谢谢!
答案 0 :(得分:0)
如果您将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()
}
}