0

I've my own custom button implemented and this works well.

import Foundation
import UIKit

class GhostYouButton: UIButton {
    required public init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)
    }

    override var isEnabled: Bool {
        didSet {
            if (self.isEnabled == false) {
                self.backgroundColor = UIColor.clear
                self.titleLabel?.textColor = Constant.disabledGrayColor
                self.tintColor = Constant.disabledGrayColor
                self.borderColor = Constant.disabledGrayColor
                self.borderWidth = 2
                self.cornerRadius = 20
            } else {
                self.backgroundColor = UIColor.clear
                self.titleLabel?.textColor = Constant.mainGreenColor
                self.tintColor = Constant.mainGreenColor
                self.borderColor = Constant.mainGreenColor
                self.borderWidth = 2
                self.cornerRadius = 20
            }
        }
    }
}

I set my GhostYouButton to be disabled in the viewDidLoad():

override func viewDidLoad() {
    self.nextButton.isEnabled = false
}

So it turns gray like I expect it to:

enter image description here

However... as you can see the title on the UIButton is faded out. I want this to be the exact same color as the border. How do I make this happen?

Rutger Huijsmans
  • 2,330
  • 2
  • 30
  • 67

2 Answers2

1

Use this line

   if (self.isEnabled == false){
    :
     self.setTitleColor(UIColor.gray, for: .normal)
    :
   }
  else{
    :
     self.setTitleColor(UIColor.green, for: . disabled)
    :
   }
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
0

Right now, it looks like you are setting the title color of the label directly. Although this works, it is not the best way to set the title color when working with a UIButton as (as you are experiencing) this property can change depending on the state of the button. Instead, you should use the setTitleColor:forState: method of UIButton which gives you more fine grain control over the style and the appearance of the button as the button stage changes: https://developer.apple.com/reference/uikit/uibutton/1623993-settitlecolor?language=swift

With this method, you can pass in a control state such as disabled, highlighted, etc: https://developer.apple.com/reference/uikit/uicontrolstate

Aaron Wojnowski
  • 6,352
  • 5
  • 29
  • 46