简体   繁体   中英

SwiftSVG doesn't display image

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)

You need to set a frame/constraints

let fistBump = UIView(SVGNamed: "ukulele") 
fistBump.frame = ////

嗨,您可以尝试将 svg 视图添加为另一个视图的子视图,您可以设置约束

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM