Vaberer / Font-Awesome-Swift

Font Awesome swift library for iOS.
MIT License
743 stars 125 forks source link

Setting FA Icon on 5th UITabbarItem causes NSException #75

Closed TCOA closed 7 years ago

TCOA commented 7 years ago

Great library - thanks! (new to Swift/iOS, though been using FA icons for many projects - glad to use them for latest one!)

Question is on setting the icon for tabs when you have more than 5.

Setting each was simple enough (the documentation states to use 'first', though we found we can do this for each one...) ` tabBarController?.tabBar.items?[0].setFAIcon(icon:.FAUser)

    tabBarController?.tabBar.items?[1].setFAIcon(icon:.FAUser)

    tabBarController?.tabBar.items?[2].setFAIcon(icon:.FAUser)

    tabBarController?.tabBar.items?[3].setFAIcon(icon:.FAUser)

    tabBarController?.tabBar.items?[4].setFAIcon(icon:.FAUser)

` Works great - IF you have only 5 tabs........

However, if you have a 6th tab, [4] no longer works....

When setting a 6th (or more) tab, iOS changes "#5" to "More" with a ... 'icon' and then the user can select which one they want (and also the user can arrange the tabs - not highly useful in our app, but a cool feature, I guess..)

So, this does NOT work..... (

tabBarController?.tabBar.items?[X].setFAIcon(icon:.FAUser) (where 'X' is 5 or more)

Doing so get an error because the array is only 5 long.....

Terminating app due to uncaught exception 'NSRangeException', reason: ' -[__NSArrayI objectAtIndex:]: index 5 beyond bounds [0 .. 4]'

How to set the icons as FA Icons for these 'More' tabs?

ranesr commented 7 years ago

@TCOA I just tried what you were trying to do. And the error you have mentioned above indicates that you haven't added the 6th view controller in the storyboard, that is why you cannot set the tab bar item for index 5.

Can you please try to add 6th view controller and link it with 6th tab bar item and see if this works? It will add last two items in "More" section.

TCOA commented 7 years ago

well, it has been awhile (and tons of code since then!) since I did that, though I found a solution that works (not my idea - found it online somewhere - sorry, no reference to where...)

class MyViewController: FormViewController{ required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) // Initialize Tab Bar Item tabBarItem = UITabBarItem(title: "TAB TITLE", image: UIImage.init(icon: .FAMoney, size: CGSize(width: 35, height: 35)), tag: 1) } ......... (finish the class)

Although you have to put this on every ViewController, it allows for setting the icon as well as the title for as many tabs as you have, and not running into the limitation of the array, so this is a much better overall solution (anyway, it worked great for me!)