简体   繁体   English

删除UIButton上的禁用色调

[英]Removing disabled tint on UIButton

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(): 我将GhostYouButton设置为在viewDidLoad()中被禁用:

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

So it turns gray like I expect it to: 因此变成灰色,就像我期望的那样:

在此处输入图片说明

However... as you can see the title on the UIButton is faded out. 但是...如您所见,UIButton的标题已淡出。 I want this to be the exact same color as the border. 我希望这是与边框完全相同的颜色。 How do I make this happen? 我如何做到这一点?

Use this line 使用此行

   if (self.isEnabled == false){
    :
     self.setTitleColor(UIColor.gray, for: .normal)
    :
   }
  else{
    :
     self.setTitleColor(UIColor.green, for: . disabled)
    :
   }

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. 尽管此方法有效,但这并不是在使用UIButton时设置标题颜色的最佳方法,因为(如您所遇到的)此属性可能会根据按钮的状态而改变。 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 相反,您应该使用UIButton的setTitleColor:forState:方法,该方法可以在按钮阶段更改时对按钮的样式和外观进行更精细的控制: 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 使用此方法,您可以传递控件状态,例如禁用,突出显示等: https : //developer.apple.com/reference/uikit/uicontrolstate

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM