0

I'm working on a project which uses SVG-images and I have read that handling sag images isn't that easy with iOS.
What I have already tried was this answer: How to display .svg image using swift. Where I changed the deprecated UIWebKit to WKWebKit, but unfortunately I get an error (nil) when it tries to unwrap the image.
Therefore I tried the second answer with the SwiftSVG-Framework.

After I installed the Framework I tried to make it work. The code compiles and has no errors but the image I use doesn't get displayed. The code I used was as follows:

let fistBump = UIView(SVGNamed: "ukulele")     // In the main bundle
self.addSubview(fistBump)

Does somebody know how to handle svg images in swift or is there a way to make SwiftSVG work?

Edit

I have tried to add a frame an constraints to my view but unfortunately this didn't fix the problem. There are still no errors shown and the picture isn't displayed.
The code I used looks like this:

let fistBump = UIView(SVGNamed: "ukulele")     // In the main bundle
self.addSubview(fistBump)
fistBump.frame = CGRect(x: 0,y: 0, width: 50, height: 50) 
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-[v0]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": fistBump]))
self.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutConstraint.FormatOptions(), metrics: nil, views: ["v0": fistBump]))

What I have also discovered is that no matter what I type into the SVGNamed-String I never get an Error so basically this works but the image is obviously not displayed:

let fistBump = UIView(SVGNamed: "test321")     // Doesn't throw an error
self.addSubview(fistBump)
Benipro98
  • 183
  • 2
  • 14

2 Answers2

0

You need to set a frame/constraints

let fistBump = UIView(SVGNamed: "ukulele") 
fistBump.frame = ////
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
  • Thanks for the reply. I've tried it to set a frame but unfortunately that didn't work. See my edited question. – Benipro98 Feb 22 '20 at 08:54
0

hi you can try add the svg view as a subview of another view which you can set the constraint