Replacement for adjustsImageWhenHighlighted in UIButton

adjustsImageWhenHighlighted is deprecated and the header says this:

@property(nonatomic) BOOL adjustsImageWhenHighlighted API_DEPRECATED("This property is ignored when using UIButtonConfiguration, you may customize to replicate this behavior via a configurationUpdateHandler"

So far I have this

    UIButtonConfiguration* buttonConfiguration =
        [UIButtonConfiguration plainButtonConfiguration];
    voice_search_button.configuration = buttonConfiguration;
    voice_search_button.configurationUpdateHandler = ^(UIButton* button) {
    };

but how do I make the image darker when highlighted/tapped? I should check if it's being highlighted and if it is then I add a tint to the UIButton? If so, what should that tint be to replicate the system color?

Thanks for the help!

Answered by Frameworks Engineer in 757613022

Typically you would go one of two ways:

  1. Use a symbol image and allow UIButton to tint it – you can also customize this tinting behavior if you prefer (either preferredSymbolConfigurationForImage or imageColorTransformer can be used to specifically target the image)
  2. Supply an alternate image, either by selecting an alternate asset or generating one (presumably once and reusing it).

The specific style that UIButton uses is basically to mix the image with black at 45% alpha, but it isn't what UIButton does by default either.

Accepted Answer

Typically you would go one of two ways:

  1. Use a symbol image and allow UIButton to tint it – you can also customize this tinting behavior if you prefer (either preferredSymbolConfigurationForImage or imageColorTransformer can be used to specifically target the image)
  2. Supply an alternate image, either by selecting an alternate asset or generating one (presumably once and reusing it).

The specific style that UIButton uses is basically to mix the image with black at 45% alpha, but it isn't what UIButton does by default either.

What would be the equivalent refactor if we want to adjustsImageWhenHighlighted to be NO?

Replacement for adjustsImageWhenHighlighted in UIButton
 
 
Q