0

All, I used fontawsome icon for UIBarButtonItem. It looks good but when tapping on it changes the icon to "?" with square. See attached images.

Normal :

search normal icon

When on tap : when tapping

let searchBtn = UIBarButtonItem(title: String.fontAwesomeIcon(name: .search), style: .plain, target: self, action: #selector(searchAction))
    searchBtn.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.tabTextColor(),NSAttributedString.Key.font: UIFont.fontAwsomeProLight(22)!], for: .normal)
    
    self.navigationItem.rightBarButtonItem = searchBtn

Any idea why it happens?

Manish
  • 608
  • 1
  • 11
  • 23

1 Answers1

3

You've only set the font to be Font Awesome for the .normal control state, but when the button is tapped, its control state changes to .highlighted. You have not set the font for this state, so the system font is used, but that Font Awesome character is not supported by the system font, so you see a question mark.

You should set the font for the highlighted state as well:

searchBtn.setTitleTextAttributes([
    NSAttributedString.Key.font: UIFont.fontAwsomeProLight(22)!
], for: .highlighted)
Sweeper
  • 213,210
  • 22
  • 193
  • 313