2017-03-01 56 views
1

我創建了以下自定義的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 
    } 
} 

這個偉大的工程!

現在我還想實現此按鈕的自定義禁用狀態。

我該如何解決這個問題?

這似乎並不工作:

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

禁用我的按鈕viewDidLoad中:

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

只需添加'self.isEnabled = FALSE' –

+0

這似乎並沒有工作。我已經更新了我的問題 –

+0

您期望的輸出是什麼,您希望在哪種狀態下禁用此 –

回答

2

我想你可以嘗試implenment的didSetisEnable的:

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

希望這可以幫助你:)

0

用於根據禁用狀態等狀態自定義WhiteGhostYouButton。您需要根據以下代碼片段在自定義類中添加drawRect方法。

class WhiteGhostYouButton: UIButton { 

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

像按鈕按下任何情況下,視重新渲染可以強制按下面的代碼片段

@IBAction func button_Pressed(sender: AnyObject) { 
     sender.setNeedsDisplay() 
    } 
相關問題