UIBarButtonItem显示?在点击时带有正方形

问题描述

所有,我为UIBarButtonItem使用fontawsome图标。看起来不错,但是点击它会将图标更改为“?”与广场。查看所附图片

正常:

search normal icon

点击时:

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

知道为什么会发生吗?

解决方法

对于.normal控件状态,您仅将字体设置为Font Awesome,但是在点击按钮时,其控件状态变为.highlighted。您尚未为此状态设置字体,因此使用了系统字体,但是系统字体不支持Font Awesome字符,因此您会看到一个问号。

您还应该为突出显示状态设置字体:

searchBtn.setTitleTextAttributes([
    NSAttributedString.Key.font: UIFont.fontAwsomeProLight(22)!
],for: .highlighted)