
Question:
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
<a href="https://i.stack.imgur.com/x21iq.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/x21iq.jpg" data-original="https://i.stack.imgur.com/x21iq.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
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?
<a href="https://i.stack.imgur.com/1F4R5.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/1F4R5.png" data-original="https://i.stack.imgur.com/1F4R5.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>
when the user click on the button, i do this in my view controller
@IBAction func foodTabled(sender: PreferenceButton) {
sender.isThisButtonSelected = !sender.isThisButtonSelected
}
Answer1: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.
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