Hello hello! I am attempting my first community app challenge. So far so good. I finally figured out how to render the SVG team logos. However, I am having difficulty resizing the logos to fit the parent.
I ended up implementing SVGKit to get the image to work. Before that, I tried the UIImage with the data object but that wasn’t working, like in the City Sights App. To implement SVGKit, I created a UIViewRepresentable view that returned a UIImageView with the SVG image, and set the frame inside that as well. Didn’t work.
Here’s the code for the UIViewRepresentable view called SVGLogo…
import SwiftUI
import SVGKit
struct SVGLogo: UIViewRepresentable {
var data:Data?
func makeUIView(context: Context) -> UIImageView {
let imgViewObj = UIImageView()
let logoSVG:SVGKImage = SVGKImage(data: data!)
imgViewObj.image = logoSVG.uiImage
imgViewObj.contentMode = .scaleAspectFit
// resize svg, doesn't work
// let containerView = UIView(frame: CGRect(x: 0, y: 0, width: 48 *
1920/1080, height: 48))
// imgViewObj.frame.size = CGSize(width: containerView.frame.width,
height:
containerView.frame.height)
// imgViewObj.contentMode = .scaleAspectFit
return imgViewObj
}
func updateUIView(_ uiView: UIImageView, context: Context) {
// update element if needed
}
}
Here’s the code for the parent of the SVGLogo…
struct StandingsRowView: View {
@EnvironmentObject var model:ContentModel
@ObservedObject var team:Teams
var body: some View {
ZStack {
Rectangle()
.foregroundColor(Color.white)
.cornerRadius(10)
.shadow(radius:10)
HStack {
HStack {
Text(String(team.ConferenceRank ?? 0))
SVGLogo(data: team.imageData ?? Data())
.frame(width: 45, height: 45)
Text(team.Name ?? "")
}.frame(height: 48)
Spacer()
HStack {
// TODO: implement if favorite, show yellow star
Text("\(String(team.Wins ?? 0)) - \(String(team.Losses ?? 0))")
}
}.padding(.horizontal)
}.frame(height:48)
}
}
I’ve looked online and tried a bunch of different methods, but still cant figure it out. Please help! thank you in advance