Mohammed Hasan
Mohammed Hasan

Reputation: 1735

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 ?

Image Description

Upvotes: 3

Views: 1189

Answers (1)

Jay Patel
Jay Patel

Reputation: 2740

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.

enter image description here

enter image description here

Upvotes: 3

Related Questions