为自定义UIButton设置禁用状态

时间:2023-01-16 20:05:09

I've created the following custom UIButton:

我创建了以下自定义UIButton:

import Foundation
import UIKit

class WhiteGhostYouButton: UIButton {

    required public init?(coder aDecoder: NSCoder) {

        super.init(coder: aDecoder)

        self.backgroundColor = UIColor.clear
        self.titleLabel?.textColor = UIColor.white
        self.borderWidth = 2
        self.borderColor = UIColor.white
        self.cornerRadius = 23
    }
}

This works great!

这很棒!

Now I also want to implement a custom Disabled state for this button.

现在我还想为这个按钮实现一个自定义的禁用状态。

How do I go about this?

我该怎么做?

This doesn't seem to work:

这似乎不起作用:

import Foundation
import UIKit

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

        super.init(coder: aDecoder)

        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
        }
    }
}

The viewDidLoad that disables my button:

禁用我的按钮的viewDidLoad:

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

2 个解决方案

#1


2  

I think you can try to implenment the didSet of isEnable:

我想你可以尝试实现isEnable的didSet:

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
           }
    }

hope this can help you :)

希望这可以帮到你 :)

#2


0  

For customizing the WhiteGhostYouButton as per the states like disabled state. You need to add drawRect method inside the custom class as per below code snippet.

用于根据禁用状态等状态自定义WhiteGhostYouButton。您需要在自定义类中添加drawRect方法,如下面的代码片段所示。

class WhiteGhostYouButton: UIButton {

    override func draw(_ rect: CGRect) {
    super.draw(rect)
    if self.isEnabled = false {
   //Customize UI here        
      } esle {
      }
   }
}

On any event like button press, re rendering of view can be forced as per below code snippet

在按下按钮的任何事件上,可以按照下面的代码片段强制重新呈现视图

 @IBAction func button_Pressed(sender: AnyObject) {
         sender.setNeedsDisplay()
    }

#1


2  

I think you can try to implenment the didSet of isEnable:

我想你可以尝试实现isEnable的didSet:

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
           }
    }

hope this can help you :)

希望这可以帮到你 :)

#2


0  

For customizing the WhiteGhostYouButton as per the states like disabled state. You need to add drawRect method inside the custom class as per below code snippet.

用于根据禁用状态等状态自定义WhiteGhostYouButton。您需要在自定义类中添加drawRect方法,如下面的代码片段所示。

class WhiteGhostYouButton: UIButton {

    override func draw(_ rect: CGRect) {
    super.draw(rect)
    if self.isEnabled = false {
   //Customize UI here        
      } esle {
      }
   }
}

On any event like button press, re rendering of view can be forced as per below code snippet

在按下按钮的任何事件上,可以按照下面的代码片段强制重新呈现视图

 @IBAction func button_Pressed(sender: AnyObject) {
         sender.setNeedsDisplay()
    }