How to get rid of blue UIButton background on different states

Have you styled your perfect UIButton just to find out you it gets blue and so do you? No worries, we can fix that!


There are many different approaches for this little annoying problem.

The first is to set the button type to custom, but then you will lose the nice system fade animation when you touch the button.

The next approach would be to make UImage backgrounds of certain colour and set them for different states but this does just not seem right to me. 

button.setImage(CustomUIImage(), for: [.highlighted, .selected])

The simplest solution in my opinion is just to change the tint color for the highlighted state, and you will get rid off the annoying blue system background.

This, as well, can be done in various ways but the simplest solution in my opinion is just adding an observer to the isHighlighted property of the button, so anytime this property changes we change the tintColor as well.

import UIKit

class CustomButton: UIButton {

    override public var isHighlighted: Bool {
        didSet {
           /* To get rid of the tint background */
            self.tintColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0)
        }
    }

}
Last updated: 1 year ago 8745