Closed Daemon-Devarshi closed 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.
@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:
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
Understood, I see where the confusion was.
I am using following code to display an icon:
But for some reasons it is not showing the icon but a question mark in place:
Am I missing anything over here?