Search code examples
iosswiftuibutton3dtouch

How to remove the greyed out look of a UIButton when hovering over


In my iPhone app, I have a bunch of UIButton which I have created in the Interface Builder. Each button has a GestureRecognizer to recognise hard pressure (3D touch) and when enough force is applied, I change the UIButton Image and do some calculation as well.

This all works fine, the only problem is that each button goes into a greyed out look when they are lightly touched (hovered over).

How do I get rid of this behavior?

Below is parts of my code:

class ViewController: UIViewController {

    @IBOutlet weak var btn: UIButton!
    ...

        let btnDeepPressGestureRecognizer = DeepPressGestureRecognizer(target: self, action: #selector(ViewController.btnDeepPressHandler(_:)), threshold: self.deepPressureThreshold)
        btn.addGestureRecognizer(btnDeepPressGestureRecognizer)

    ...
    func btnDeepPressHandler(_ value: DeepPressGestureRecognizer) {
        if value.state == .began {
            ...
        } else if value.state == .ended {
            ...
        }
    }

Screen shot of one of the buttons attribute inspector:

enter image description here


Solution

  • I can only think of two things right now:

    you can try this:

    yourButton.adjustsImageWhenHighlighted = false
    

    and this:

    yourButton.adjustsImageWhenDisabled = false
    

    According to documentation:

    When the Shows Touch On Highlight (showsTouchWhenHighlighted) option is enabled, the button adds a white glow to the part of a button that the user touches.

    When the Highlighted Adjusts Image (adjustsImageWhenHighlighted) option is enabled, button images get darker when it is in the highlighted state.

    When the Disabled Adjusts Image (adjustsImageWhenDisabled) option is enabled, the image is dimmed when the button is disabled.

    Since it happens on light touch, your button should be highlighted and glow but it's dimmed so maybe it's somehow disabled.