Search code examples
ioscalayeruicontrol

Setting CALayer color in raywenderlich.com article


I have a question about this article https://www.raywenderlich.com/7595-how-to-make-a-custom-control-tutorial-a-reusable-slider

I substitute

var trackTintColor = UIColor(white: 0.9, alpha: 1) {
    didSet {
      trackLayer.setNeedsDisplay()
    }
  }

with

var trackTintColor = UIColor(red: 56, green: 81, blue: 117, alpha: 1) {
    didSet {
      trackLayer.setNeedsDisplay()
    }
  }

in RangeSlider.swift .

But the color that I want won't appear and RangeSlider have no background color.

Could you let me know why? And How could I make RangeSlider have the color that I want?

Thanks for reading.


Solution

  • The value range of the UIColor components must be between 0.0 and 1.0 which is pretty obvious in the first example.

    You could divide the UInt8 values by 255 (as floating point numbers to get correct floating point results)

    var trackTintColor = UIColor(red: 56.0/255.0, green: 81.0/255.0, blue: 117.0/255.0, alpha: 1.0) { ...
    

    or pre-calculate the values, or define the color in the asset catalog