Advertisement
Guest User

Button extension for ColorStates

a guest
Jul 25th, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. extension UIControlState : Hashable {
  2. public var hashValue: Int {
  3. return Int(self.rawValue)
  4. }
  5. }
  6.  
  7. class MyButton : UIButton {
  8.  
  9. public override var state: UIControlState {
  10. get {
  11. let currentState = super.state
  12.  
  13. self.backgroundColor = self.backgroundColorsForState[currentState]
  14.  
  15. return currentState
  16. }
  17. }
  18.  
  19. private var backgroundColorsForState : [UIControlState : UIColor] =
  20. [UIControlState.normal : UIColor.clear,
  21. UIControlState.highlighted : UIColor.clear,
  22. UIControlState.disabled : UIColor.clear,
  23. UIControlState.selected : UIColor.clear,
  24. UIControlState.focused : UIColor.clear,
  25. UIControlState.application : UIColor.clear,
  26. UIControlState.reserved : UIColor.clear]
  27.  
  28. public func setBackgroungColor(_ color: UIColor?, for state: UIControlState) {
  29. if let col = color {
  30. self.backgroundColorsForState[state] = col
  31. } else {
  32. self.backgroundColorsForState[state] = UIColor.clear
  33. }
  34. self.state
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement