简体   繁体   中英

UIBarButtonItem shows ? with square when tap on it

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 :

搜索普通图标

When on tap : 敲击时

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?

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM