-1

This is a normal state of my UIButton: normal state

But when I press on the UIButton, its highlight color should be changed to a different one. And here is what I have: pressed state

So, if you can notice, my white text becomes overlapped with the new color. But what I should have as a result is just a different color of the highlight and always the white text. Like so: expected result

What I am doing so far is:

  1. In Attribute Inspector in my xib file I changed the Highlight Color to a new one.
  2. I also use updateButton.setTitleColor(.white, for: .highlighted) in my code but it doesn't help.

I've also taken a look at this question: UIButton background color overlaps text on highlight but the accepted answer didn't help much.

Lilya
  • 495
  • 6
  • 20

3 Answers3

0

This myButton.setTitleColor(.white, for: .normal) doesnt change title color when highlighted , Its for normal state. If you want to change textColor when button is highlighted, you need to use myButton.setTitleColor(.white, for: .highlighted)

Also when you change state config in storyboard enter image description here

you can set Text Color

If its a custom Button you can override isHighlighted method and do what you need like

class MyButton : UIButton {
override var isHighlighted: Bool{
    didSet {
        tintColor = isHighlighted ? UIColor.red : UIColor.white
        backgroundColor = UIColor.blue 
        .....
        // do what you need
    }
}
Omer Tekbiyik
  • 4,255
  • 1
  • 15
  • 27
0

Similar to myButton.setTitleColor(.white, for: .normal) please try setting title colour for .highlighted state. That should work for you.

You can refer: https://developer.apple.com/documentation/uikit/uibutton/1623993-settitlecolor

Shan
  • 41
  • 1
  • Hi @Shan and thanks for your comment. This is exactly what I was doing. I've updated my question. – Lilya Jul 02 '20 at 13:32
0

As your button is subclass of UIView class CellButton: UIView {}, you need to handle touches begin and end events in custom CellButton and handle the appearance of the view. If it was subclass of UIButton then it would have handled automatically for you :)

You can also try with gestures, for more info you can refer: https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/handling_uikit_gestures/handling_tap_gestures

Shan
  • 41
  • 1
  • Thank you @Shan ! It's actually a subclass of UIButton, I just wasn't attentive enough. – Lilya Jul 02 '20 at 13:54