Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI SVG in TabBar

Tags:

svg

swiftui

Now that SwiftUI can use SVG images directly, I have tried to use a set of SVG files as icons for a TabBar. Using typical TabBar code:

            TabView(selection: $lastTab){
                VendorView()
                    .tabItem {
                        Image ("store")
                            .renderingMode(.template)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width: 25.0, height: 25.0)
                        Text("Vendor")
                }
                .tag(0)
                ...

Unfortunately this does not seem to work as the SVG image is never resized to the requested 25x25 size.

enter image description here

Has anyone else experienced this and if so, any workarounds?

like image 483
Ferdinand Rios Avatar asked Dec 05 '25 18:12

Ferdinand Rios


1 Answers

The best way to handle the issue of SVGs in the TabView as an Image() is to do the following steps:

  1. resize your SVG in Sketch/Figma or other program to 22x22 / 24x24...

  2. Bring said resized SVG to assets and in your Inspector select:

    1. Render as: Template Image

    2. Resizing: Preserve vector data

    3. Scales: Single Scale

  3. No need to use .resizable() nor .frame()

I have learnt new tips and original answer by: Mark Moeykens https://stackoverflow.com/a/37627080/15382735

like image 164
3thancr0wn Avatar answered Dec 08 '25 19:12

3thancr0wn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!