UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How do you add a custom .svg symbol to SwiftUI?

Forums > SwiftUI

I had a designer create a custom sysmbol using Apple's SF Symbols documentation. They gave me an .svg file that I dutifuly added to the asset catalogue (again using Apple's documentation) but when I tried using it in a view it does not appear to be rendering as expected so is there some other step required to make these things work in SwiftUI?

3      

How is it rendering?

All you have to do is import your properly formatted SVG (and "properly formatted" is very important) into your assets catalog, then you can use it in your code like this:

let img = UIImage(named: "yourImageName")

You can adjust various properties of the image using SymbolConfiguration, as per Apple's docs.

For mine, I created an extension on UIImage to make it easier to create an image in different scales.

extension UIImage {
    static func catHeadImage(filled: Bool = false, scale: SymbolScale) -> UIImage {
        let config = UIImage.SymbolConfiguration(scale: scale)
        return (UIImage(named: filled ? "cat.head.fill" : "cat.head")!.withConfiguration(config).withRenderingMode(.alwaysTemplate))
    }
}

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.