thii / FontAwesome.swift

Use FontAwesome in your Swift projects
MIT License
1.57k stars 266 forks source link

UINavigationItem icon not appearing #254

Closed Daemon-Devarshi closed 4 years ago

Daemon-Devarshi commented 4 years ago

I am using following code to display an icon:

@IBOutlet var drawerMenuItem: UIBarButtonItem!

override func viewDidLoad() {
        super.viewDidLoad()

        let attributes = [NSAttributedString.Key.font: UIFont.fontAwesome(ofSize: 18, style: .regular)]
        drawerMenuItem.setTitleTextAttributes(attributes, for: .normal)
        drawerMenuItem.title = String.fontAwesomeIcon(name: .github)
}

But for some reasons it is not showing the icon but a question mark in place:

Screenshot 2020-04-30 at 10 32 05 PM

Am I missing anything over here?

MatrixSenpai commented 4 years ago

Don't tie your navigation item to an IBOutlet as it is completely useless and there is no reason to re-implement it. Doing so could cause unexpected behavior. I repeat again, do not re-implement navigationItem or any of its children. Always use their built in accessors via self.navigationItem

You should instead use the member leftBarButtonItem. For more information on what this is and why it's important, see Apple's Documentation here

Try this code sample instead:

var leftButton: UIBarButtonItem {
  let b = UIBarButtonItem(title: String.fontAwesomeIcon(name: .github), style: .plain, target: <#T##Any?#>, action: <#T##Selector?#>)
  b.setTitleTextAttributes([.font: UIFont.fontAwesome(ofSize: 18, style: .regular)], for: .normal)
  b.setTitleTextAttributes([.font: UIFont.fontAwesome(ofSize: 18, style: .solid)], for: .highlighted)
  return b
}

override func viewDidLoad() {
  super.viewDidLoad()

  navigationItem.leftBarButtonItem = leftButton
}

Do note that the placeholders left in the UIBarButtonItem initializer are left there for your convenience. Fill them in with the actions supplied to your button.

You should also set text attributes for .normal as well as .highlighted. Failure to do so will give you the unknown unicode icon when your bar button is tapped.

Daemon-Devarshi commented 4 years ago

@MatrixSenpai the solution which you proposed did not work for me either.

Having said that I am able to find the solution. Posting here so that in future it may help someone who faces the same problem:

Looks like the display of font awesome icon on UIBarButtonItem depends upon the style. For example in my case I was using the style as regular for GitHub icon, however I got it working by using the style as brands. In other cases it can be different, as an example if I am using bars icon then it is displayed only if the style is solid.

Also the point which you overemphasised is flawed. Neither there is any relevant content justifying it in the Apple Documentation which you referenced. As a matter of fact you are also creating a separate instance of UIBarButtonItem, programmatically, which I did through storyboard. On googling noticed that this practise is not very uncommon:

MatrixSenpai commented 4 years ago

Ah, seems I missed which icon you were using. You're correct, the font you were using was incorrect, and checking that is very important :) Brands font includes the GitHub icon, while the code you posted has you using the regular font, an important distinction.

I should rephrase my earlier advice, since it seems you've edited your code. Referencing a bar button item is, while redundant, safe to do. However, referencing a nav item is unsafe. The best and safest practice is simply to use the built-in accessor already included in UIViewController

Daemon-Devarshi commented 4 years ago

Understood, I see where the confusion was.