简体   繁体   中英

How to make a custom UIBarButtonItem Class in Xcode + Swift 4?

I'm trying to make a custom UIBarButtonItem class to just change the font and the color of the Bar Button I have added to my View controllers. and I have more than 20 Buttons that need to be changed.

How could I do this by just adding a custom UIBarButtonItem Class from the (Identity Inspector) story board ?

图片描述

Create class for 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)
    }
}

Set CustomBarButton class to UIBarButtonItem from Identity Inspector.

在此处输入图片说明

在此处输入图片说明

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