Using Xcode/Swift, how can I use an SF Symbol as a tab bar icon in assets? The asset field won't accept the .svg file

Viewed 5092

I can't use an SF Symbol as an asset for a tab bar icon.

I tried using the GUI to drag and drop the file. I'm unsure of how to add it programatically.

I expected to be able to drag/drop but it won't accept the .svg file. I add a new symbol set but that doesn't work as the tab bar icon asset.

3 Answers

If you are using the new Xcode 11 beta, the new SF Symbols are already included and you do not need to import the .svg files.

Open your Storyboard file, click on the tab bar icon (not the one in the tab bar controller).

Now open the attributes inspector on the right hand side and open the combo box at "Bar Item" -> "Image". Now there will be the SF Symbols in this list as in the screenshot below.

Screenshot Steps 1-3 Screenshots Step 4

The following code works for setting the system icon for UITabBarItem with UIKit and Xcode 11 or 12 via Swift:

var yourTabViewController: YourTabViewController?
let iconConfig = UIImage.SymbolConfiguration(scale: .large)
let gearIcon = UIImage(systemName: "gearshape.fill", withConfiguration: iconConfig)
let settingsTabBarItem = UITabBarItem(title: "Settings", image: gearIcon, tag: 0)
yourTabViewController = storyboard.instantiateViewController(withIdentifier: YourTabViewController.identifier) as? YourTabViewController
yourTabViewController?.tabBarItem = settingsTabBarItem

Reference that helped me

Related