can't change the button color when not selected


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 }


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


  • How to make a rectangle with mouse listeners and add them to JFrame?
  • How to rotate an ellipse around the center of program?
  • UIColor SetFill doesn't work
  • Combining histograms with a density plot in R
  • Number axis setTickUnit to contain a specified number
  • What the easiest way to animate a Path as an object traverses it?
  • Create a new variable from the minimum in R
  • Change color to a title of a button in Swift [duplicate]
  • html/javascript canvas change set value to a user input
  • Typeerror occours when I tried to get a graphic from CSV file in python
  • How to implement delete functionality for images in different positions in iCarousel iphone
  • Matplotlib line width based on axis, not on points
  • Why don't added sublayers show up in screenshot?
  • SKPhysicsJointFixed doesn't keep nodes together while moving one
  • Get touch points in UIScrollView through UITapGestureRecognizer
  • using multiple labels with tooltip plugin
  • Whats an effective HashCode() for small x, large y values?
  • animating a custom CALayer property
  • Drag and drop without removing UIButton
  • Android: Button background XML sometimes loses alpha when setting color filter
  • String not fitting in label which is in a subview for swift
  • CSS animation do not work for svg in
  • Can I override some attribute of drawable shape?
  • JsonConvert.DeserializeObject, Index was outside the bounds of the array
  • Recreate Apple Watch fitness tracker ‘progress’ bar - gradient on CAShapeLayer stroke
  • Drag and reorder - UICollectionview with sections
  • jersey/tomcat Description The origin server did not find a current representation for the target res
  • Unable to connect to OnVif enabled camera using C#
  • .NET video play library which allows to change the playback rate?
  • Plotting densities in R
  • Getting different value with placeholder over CPU/GPU
  • Update Google Maps traffic layer without page reloading
  • Installing iPhone App to iPhone
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Javascript + PHP Encryption with pidCrypt
  • JTable with a ScrollPane misbehaving
  • Getting Messege Twice Using IMvxMessenger
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?