我如何使用 PocketSVG 在 SwiftUI 中显示整个 SVG 图像?
How could I use PocketSVG to display the entire SVG image in the SwiftUI?
我的源码很简单如下:
//
// 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()
}
}
但是显示是这样的:
看起来视图比屏幕大。
正常的应该是这样的:
我该怎么办?谢谢!
如果您将 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()
}
}
我的源码很简单如下:
//
// 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()
}
}
但是显示是这样的:
看起来视图比屏幕大。
正常的应该是这样的:
我该怎么办?谢谢!
如果您将 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()
}
}