NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

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?

   

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

   

Hacking with Swift is sponsored by RevenueCat

SPONSORED Building and maintaining in-app subscription infrastructure is hard. Luckily there's a better way. With RevenueCat, you can implement subscriptions for your app in hours, not months, so you can get back to building your app.

Try it for free

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

Reply to this topic…

You need to create an account or log in to reply.

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.