Search code examples
iosswiftuibarbuttonitem

UIFont changes when tapped on bar button item swift


I set setTitleTextAttributes for barButtonItem, UIColor, Custom Font. When I tapped on barButton, during that time when I hold barButton, font changes to the system one, after I let go barButton it return to the custom font.I don't use tintColor settings. Why this happen?

May anyone help me?

This is how I set properties:

@IBOutlet weak var saveBarButton: UIBarButtonItem! { 
    didSet {
        saveBarButton.title = "SAVE"
        saveBarButton.setTitleTextAttributes([NSAttributedString.Key.font:
        UIFont.textStyle5], for: .normal)
    } 
}

I use custom UIFont. Thanks in advance.


Solution

  • You need to set the same font for the highlighted state as well:

    saveBarButton.setTitleTextAttributes([NSAttributedString.Key.font: UIFont.textStyle5], for: .highlighted)