lmiguelvargasf lmiguelvargasf - 2 years ago 370
Swift Question

Swift: Use a FontAwesome icon as a UITabBarItem image

I would like to use some icons in the FontAwesome Swift library as UITabBarItem images. I have been able to set the UITabBarItem title, but I am not able to set the UITabBarItem image. My code is as follows:

class AEVTabBarController: UITabBarController {

var itemLabels = ["Promos", "Marcas", "Amigos", "Cerca de mí", "Más"]

override func viewDidLoad() {

let tabItems = self.tabBar.items as [UITabBarItem]!

for index in 0..<itemLabels.count {
let currentItem = tabItems[index] as UITabBarItem
currentItem.title = itemLabels[index]
currentItem.image = String.fontAwesomeIconWithName(FontAwesome.Money) as UIImage

What the compiler is telling me is that 'String' is not convertible to 'UIImage'. Is there a way to convert a String into a UIImage or is there another way to solve this?

Thanks in advance.

Answer Source

Per the readme on that library you linked:

currentItem.image = UIImage.fontAwesomeIconWithName(.Money, , textColor: UIColor.blackColor(), size: CGSizeMake(30, 30))

The function you used returns a string that is the text shortcut for the icon. This would be useful for a label or button text that's using FontAwesome. What you're trying to do is set the image - I would re-read the Readme, and just get acquainted with the built in functions of that library.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download