如何在Xcode + Swift 4中创建自定义UIBarButtonItem类?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Xcode + Swift 4中创建自定义UIBarButtonItem类?相关的知识,希望对你有一定的参考价值。

我正在尝试制作一个自定义的UIBarButtonItem类来改变我添加到View控制器的Bar Button的字体和颜色。我有20多个按钮需要更改。

我怎么能通过从(Identity Inspector)故事板添加自定义UIBarButtonItem类来做到这一点?

Image Description

答案

为UIBarButtonItem创建类

class CustomBarButton: UIBarButtonItem {
    override func awakeFromNib() {
        style = .plain
        tintColor = .red

        //Set font name and size
        guard let font = UIFont(name: "Helvetica-Bold", size: 19) else {
            return
        }

        setTitleTextAttributes([NSAttributedStringKey.font:font], for: .normal)
    }
}

从Identity Inspector将CustomBarButton类设置为UIBarButtonItem。

enter image description here

enter image description here

以上是关于如何在Xcode + Swift 4中创建自定义UIBarButtonItem类?的主要内容,如果未能解决你的问题,请参考以下文章