2

In my app, I have a button with a text title next to a button that's an image. I want the color scheme of both buttons to match. I create the button with the image like this:

let button1 = UIButton()
button1.setImage(
UIImage(named: "button1")?.withRenderingMode(.alwaysTemplate), for: .normal)
button1.tintColor = UIColor.green

This creates the effect that I want on both buttons, i.e. the button is green, then when it's highlighted it gets tinted to a darker, black-ish green. I tried creating the text button the same way:

let button2 = UIButton()
button2.setTitle("button2", for: .normal)
button2.tintColor = UIColor.green

But, in this case, setting the tint color doesn't change the color of the button's title/text (it remains white even when highlighted). My solution to this is as follows:

let button2 = UIButton()
button2.setTitle("button2", for: .normal)
button2.setTitleColor(UIColor.green, for: .normal)
button2.setTitleColor(UIColor(red: 0x23 / 255.0,
                              green: 0x34 / 255.0,
                              blue: 0x16 / 255.0,
                              alpha: 1.0), for: .highlighted)

Essentially, I've estimated the color that the image gets tinted to when it's highligted and set the text color to match. This works fine, but it bothers me that I only have an approximation; ideally, I would want the system to tint the text color for me when the button is highlighted in the same way that it tints the image. I get that this is a really small problem and that fixing it probably won't noticeably improve the app, but I'd still like to know if there's a way to tint a button with a text title "automatically" (as opposed to hardcoding the tint).

2
  • As I understand, you want text color change automatically when you change tintColor. Is it right? Commented Apr 12, 2018 at 3:50
  • I want it to change automatically when the button is highlighted based on the color I specify for the normal state. Commented Apr 12, 2018 at 4:02

2 Answers 2

5

Tint color is property of UIView, which doesn't have a state. State is property of UIControl(Button's parent class). Means that you cannot change the tint on the bases of button's state. You can only change properties mentioned seen in this screen shot on the basis of button's state by default.

enter image description here

Also the

darker, black-ish green

color your getting that the default behaviour of button to change background color to show highlighted state

Solution : CustomButton

Create a custom UIButton

class MyButton : UIButton {
    override var isHighlighted: Bool{
        didSet {
            tintColor = isHighlighted ? UIColor.green : UIColor.red
            // do additional work here according to your need
        }
    }

    override var isSelected: Bool {
        didSet {
            // do changes according to you need
        }
    }
}

You can also set the properties mentioned in above image programmatically.

button.setTitleColor(UIColor.green, for: .normal)
button.setTitleColor(UIColor.red, for: .highlighted)
button.setBackgroundImage(yourBackgroundImage, for: .normal)
Sign up to request clarification or add additional context in comments.

Comments

0
let button2 = UIButton()
button2.addTarget(self, action: #selector(self.pressed), for: [.touchDown])
button2.addTarget(self, action: #selector(self.released), for: [.touchDragExit, .touchUpInside, .touchUpOutside, .touchCancel])


func pressed() {
   // set colour
}

func released() {
   // set colour
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.