title: Custom UIButton isSelected background color not working as expected (IOS / Swift)
body: <p>I have a custom <code>UIButton</code> class where this code is not working as expected:</p>
  
  <pre><code>    override var isSelected: Bool {
          didSet {
              backgroundColor = isSelected ? UIColor.blue : UIColor.white
          }
      }
  </code></pre>
  
  <p>When I set <code>isSelected</code> in code, the background does NOT change... it stays white.  I know that the actual boolean value <code>isSelected</code> true/false IS actually being toggled properly, though.</p>
  
  <p>The code inside didSet IS executing (I have verified with print debugging), but somehow the background is not being set.</p>
  
  <p>Interestingly, I have also overridden the <code>isHighlighted</code> method in exactly the same way, which works fine.</p>
  
  <p>Here is the full code for the custom button class.  <strong><em>FEEL FREE TO CUT/PASTE THIS INTO YOUR XCODE AND PLAY WITH IT</em></strong>:</p>
  
  <pre><code>import UIKit
  
  @IBDesignable
  class PlusMinusButton: UIButton {
  
      @IBInspectable
      var highlightColor: UIColor = .yellow { didSet { setNeedsDisplay() } }
  
      @IBInspectable
      var fillColor: UIColor = .yellow { didSet { setNeedsDisplay() } }
  
      @IBInspectable
      var strokeColor: UIColor = .yellow { didSet { setNeedsDisplay() } }
  
      @IBInspectable
      var plusOrMinus: Bool = true { didSet { setNeedsDisplay() } }
  
  
      func getPath() -&gt; UIBezierPath {
  
          let G = bounds.width / 5
  
          let path = UIBezierPath()
  
          switch plusOrMinus {
  
          case true:
  
              let startPoint = CGPoint(x:2*G,y:G)
              path.move(to: startPoint)
              path.addLine(to: CGPoint(x:2*G,y:2*G))
              path.addLine(to: CGPoint(x:G,y:2*G))
              path.addLine(to: CGPoint(x:G,y:3*G))
              path.addLine(to: CGPoint(x:2*G,y:3*G))
              path.addLine(to: CGPoint(x:2*G,y:4*G))
              path.addLine(to: CGPoint(x:3*G,y:4*G))
              path.addLine(to: CGPoint(x:3*G,y:3*G))
              path.addLine(to: CGPoint(x:4*G,y:3*G))
              path.addLine(to: CGPoint(x:4*G,y:2*G))
              path.addLine(to: CGPoint(x:3*G,y:2*G))
              path.addLine(to: CGPoint(x:3*G,y:G))
              path.addLine(to: startPoint)
  
          case false:
  
              let startPoint = CGPoint(x:G,y:3*G)
  
              path.move(to: startPoint)
              path.addLine(to: CGPoint(x:4*G,y:3*G))
              path.addLine(to: CGPoint(x:4*G,y:2*G))
              path.addLine(to: CGPoint(x:G,y:2*G))
              path.addLine(to: startPoint)
  
          }
  
          path.close()
  
          return path
  
      }
  
      // see how is method is exactly the same as the next?  weird, huh?
      override var isHighlighted: Bool {
          didSet {
              backgroundColor = isHighlighted ? highlightColor : UIColor.white
          }
      }
  
      override var isSelected: Bool {
          didSet {
              print("This line of code is executed!!")
              backgroundColor = isSelected ? UIColor.blue : UIColor.white
          }
      }
  
      override func draw(_ rect: CGRect) {
  
          let path = getPath()
  
          fillColor.setFill()
  
          path.fill()
  
          strokeColor.setStroke()
  
          path.stroke()
  
      }
  }
  </code></pre>
  
  <p>Thanks for any help!  I bet it's something real dumb!  Merp! :B</p>