gpt4 book ai didi

svg - 如何使用 PocketSVG 在 SwiftUI 中显示整个 SVG 图像?

转载 作者:行者123 更新时间:2023-12-04 10:57:55 27 4
gpt4 key购买 nike

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

//
// 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()
}
}

但它显示就像这样:
enter image description here
看起来 View 比屏幕大。
正常的应该是这样的:
enter image description here
我怎么办?谢谢!

最佳答案

如果您嵌入 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()
}
}

关于svg - 如何使用 PocketSVG 在 SwiftUI 中显示整个 SVG 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59060885/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com