Change colour of dark grey highlight when holding down custom UIButton?
Asked Answered
A

5

41

I have a custom UIButton which is a cloud, transparent black and white .png file, with no down state, just one image. When tapping and holding the finger over it, it turns dark grey. I'm trying to change that dark grey to something a little less oppressive. The button is out in the open in a view, not in a tab bar, tool bar, or navigation controller.

I've already tried setting tintColor (which the documentation helpfully informs me is only suitable for 'some' types of buttons, which no indication as to which).

I've also tried changing everything I can find in Interface Builder relating to highlight colours, default states, etc. Nothing has made a difference at all.

I've even tried setting the button's own image for its UIControlStateHighlighted state, but even this causes the dark grey overlay to appear when I hold my finger over it.

How can I change that colour? I've looked at numerous other issues here on SO and have been unable to find a solution that works for me. Any help would be greatly appreciated!

EDIT: I Solved the problem using a category of UIImage which adds a method that uses CoreGraphics to apply a tint to a provided UIImage. I then set THAT image as the highlight, and all is well. Seems a lot of hoop-la to change a colour Apple should've let us change, but c'est la vie.

Apogamy answered 12/4, 2013 at 7:58 Comment(0)
D
78

You said you set a custom image for the UIControlStateHighlighted state. This should disable the default behaviour.

If you still have problems you can disable this effect by setting the adjustsImageWhenHighlighted property to NO and use whatever custom effect you want.

Dior answered 12/4, 2013 at 8:0 Comment(2)
I just made an edit: setting a custom image for UIControlStateHighlighted really should work. I would try this again and make sure you didn't make some mistake.Dior
I think it's setting it but not stretching it properly, I'm doing some stuff with resizing the image based on the text inside it. It shows the middle as white, but the dark grey around the outsides, so guessing the image isn't filling the original's space. Which is odd, since I'm setting it to itself.Apogamy
L
38

If adjustsImageWhenHighlighted = NO is not working, set Button-Type to Custom (IB or programmatically).

Default Button-Type: System, changes behavior of highlighted button.

Logical answered 4/11, 2013 at 16:6 Comment(1)
You saved my life. Interface Builder's default type for UIButton is system. Really strange, uh?Fescennine
C
13

Swift 3:

myButton.adjustsImageWhenHighlighted = false
Complicated answered 14/3, 2017 at 12:58 Comment(1)
worked. I don't want to image color darker when it gets highlighted. An image should be as it is when gets highlighted.Carollcarolle
M
1

I was having a similar issue with a custom UIButton when the button was highlighting in grey every time it was pressed. I solved that problem by subclassing UIButton and in the implementation I overrode a single method, (void)setHighlighted: method and kept it empty:

- (void)setHighlighted:(BOOL)highlighted
{
   // Leave empty to prevent super from doing whatever
   // that it is doing to show the grey highlight.
}

That stopped any type of highlighting as I was not doing anything in the method. It's a better approach if all that you're trying to do is remove any highlighting effect.

So in your code, create a subclass of UIButton, override the setHighlighted method, and then make your custom button a subclass of this custom class.

Moten answered 19/7, 2013 at 3:0 Comment(1)
Perhaps the super is also doing more than just messing with the color change though. Would be wary of doing this.Carrera
S
1

You can write a custom button that does it

class ActionButton: UIButton {

  var originalBackgroundColor: UIColor!

  override var backgroundColor: UIColor? {
    didSet {
      if originalBackgroundColor == nil {
        originalBackgroundColor = backgroundColor
      }
    }
  }

  override var isHighlighted: Bool {
    didSet {
      guard let originalBackgroundColor = originalBackgroundColor else {
        return
      }

      backgroundColor = isHighlighted ? originalBackgroundColor.darken() : originalBackgroundColor
    }
  }
Shelia answered 27/3, 2017 at 10:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.