1

i have many custom buttons, and i want to set a color for state selected and not selected.

this is my drawrect

   override func drawRect(rect: CGRect) {
        print("status = \(self.selected)")
        if self.selected {
            self.titleLabel?.textColor = UIColor(red: 255.0, green: 255.0, blue: 255.0, alpha: 1.0)
        }else {
            self.titleLabel?.textColor = UIColor(red: 166.0, green: 142.0, blue: 83.0, alpha: 1.0)
        }

        let path = UIBezierPath()
        path.moveToPoint(CGPoint(x: self.bounds.minX, y: self.bounds.maxY))
        path.addLineToPoint(CGPoint(x: self.bounds.maxX, y: self.bounds.maxY))
        path.closePath()
//        path.addLineToPoint(center)
        UIColor.blackColor().setStroke()
        path.lineWidth = 3.0
        path.stroke()
    }

   private var isSelectedValue = false
    var isThisButtonSelected : Bool {
        get {
            return isSelectedValue
        }
        set {
            isSelectedValue = newValue
            selected = newValue
            switch newValue {
            case true:

                self.selected = true
                rightImageView?.image = UIImage(named: "selection-preferences")
                break;
            case false:
                self.selected = false

                rightImageView?.image = nil
                break;
            }
        }
    }

i have many buttons, and as you see, i print the selected status.

the result is always false (as you will see in the screenshot)

my problem is that the color is white when not selected even though i say in the draw rect that if not select, make specific color

enter image description here

but it work good when i select a button so the color becomes white, as you see, but why when i unselect the color not change?

enter image description here

when the user click on the button, i do this in my view controller

 @IBAction func foodTabled(sender: PreferenceButton) {
        sender.isThisButtonSelected = !sender.isThisButtonSelected
    }
rmaddy
  • 314,917
  • 42
  • 532
  • 579
sarah
  • 1,201
  • 1
  • 9
  • 29
  • @elprup i add it and still the same – sarah Nov 21 '15 at 08:47
  • In an unrelated observation, in Swift, there is no [implicit fallthrough](https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html#//apple_ref/doc/uid/TP40014097-CH9-ID130) in `switch` statements. No `break` is needed here. – Rob Nov 21 '15 at 17:45

2 Answers2

1

Like alpha, the red/green/blue values to UIColor are between 0.0 and 1.0. You must divide your values by 255.0.

--

In addition to setTitleColor:forState:, you also have setBackgroundImage:forState:, to control the the presence or absence of that additional image (though you'd refactor that image to cover the whole background). With judicious use of those two methods, you can probably streamline this button class, altogether, retiring isSelectedValue and just using the existing selected property.

Furthermore, you can render that line under the button as a solid-colored subview of the button. If you did that, you could retire drawRect, too.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • i am so loky to see you in this website, you are the best over the world – sarah Nov 22 '15 at 14:41
  • can you please look for me in my new question? https://stackoverflow.com/questions/33855946/uibezierpath-not-update-as-expected – sarah Nov 22 '15 at 14:41
0

Use following statement

 self.setTitleColor(UIColor(red: 166.0, green: 142.0, blue: 83.0, alpha: 1.0), forState: UIControlState.Normal)
 self.setTitleColor(UIColor(red: 255.0, green: 255.0, blue: 255.0, alpha: 1.0), forState: UIControlState.Selected)

instead of

if self.selected {
        self.titleLabel?.textColor = UIColor(red: 255.0, green: 255.0, blue: 255.0, alpha: 1.0)
    }else {
        self.titleLabel?.textColor = UIColor(red: 166.0, green: 142.0, blue: 83.0, alpha: 1.0)
    }

And no need to check state, it will automatically set colour according to its state

VRAwesome
  • 4,721
  • 5
  • 27
  • 52