ios - bar按钮项目文本在moonIcon中选择时会改变,就像swift 4中的Font Awesome一样

标签 ios swift4 font-awesome uibarbuttonitem uibarbuttonitemstyle

我使用此代码在我的应用程序中为navigationController中的UIBarButtonItem使用moonIcon Font,例如Awesome字体

let menuButton = UIBarButtonItem(title: publicSVGAssets().menu, style: UIBarButtonItemStyle.plain, target: self, action: #selector(menuAction))
menuButton.setTitleTextAttributes(NSDictionary(dictionary: [NSAttributedStringKey.font : UIFont(name: "icomoon", size: 25)!, NSAttributedStringKey.foregroundColor : UIColor.red]) as? [NSAttributedStringKey : Any], for: [])
self.navigationItem.rightBarButtonItem = menuButton

这会工作得很好,但问题是,当用户选择按钮时,图标会发生如下图所示的变化 as you see in the photo the right Bar Button Image will change to question mark But I want that Menu Image like when the button Does not selected

最佳答案

它可能很简单,因为仅当按钮的状态为 .normal (如您指定)时才应用字体属性。

您可能需要指定按钮的所有状态都应用这些标题文本属性,如下所示:

let title = publicSVGAssets().menu
let style = UIBarButtonItemStyle.plain
let selector = #selector(menuAction)
let menuButton = UIBarButtonItem(title: title, style: style, target: self, action: selector)

let font = UIFont(name: "icomoon", size: 25)
let attributesDictionary: [NSAttributedStringKey: Any]? = [NSAttributedStringKey.font: font!, NSAttributedStringKey.foregroundColor : UIColor.red]
menuButton.setTitleTextAttributes(attributesDictionary, for: .normal)
menuButton.setTitleTextAttributes(attributesDictionary, for: .selected)
menuButton.setTitleTextAttributes(attributesDictionary, for: .highlighted)

self.navigationItem.rightBarButtonItem = menuButton

点击按钮将更改其状态。这可能就是为什么当您点击它时您会看到它改变图标。

注意:如果您需要在其他状态下显示按钮(例如禁用),您还需要为该状态分配相同的标题文本属性。

...例如:

menuButton.setTitleTextAttributes(attributesDictionary, for: .disabled)

(注意:这与 @Satish 给出的答案类似)。

关于ios - bar按钮项目文本在moonIcon中选择时会改变,就像swift 4中的Font Awesome一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52721492/

相关文章:

ios - class func 与 only func 的区别

ios - 调用 collectionView(_ :layout:sizeForItemAt:) in Swift 4

html - 如何拆分按钮内的文本

unicode - SCSS 处理器 Unicode 输出不正确

ios - Objective-C 是否超时更改

iOS:Main.storyboard 引用初始 View Controller

swift4 - 如何防止 Swift 4 重叠或独占访问错误?

html - 如何在使用 font awesome 时更改字体大小?

ios - 带有 Alamofire 4 正文数据的 POST 请求

ios - 如何删除节中的 TableView 行