init(colorLiteralRed:,green:,blue:,alpha:) deprecated in Swift 4

Viewed 14925
let startingColorOfGradient = UIColor(colorLiteralRed: 255/255, green: 
255/255, blue: 255/255, alpha: 1.0).cgColor
let endingColorOFGradient = UIColor(colorLiteralRed: 251/255, green: 
247/255, blue: 234/255, alpha: 1.0).cgColor
let gradient: CAGradientLayer = CAGradientLayer()

Error:

'init(colorLiteralRed:green:blue:alpha:)' was obsoleted in Swift 4.0 (Swift._ExpressibleByColorLiteral)

How do I use gradient color if init(colorLiteralRed:,green:,blue:,alpha:) is deprecated in Swift 4?

4 Answers

Another way : https://developer.apple.com/documentation/uikit/uicolor/1621925-init

init(displayP3Red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)

Initializes and returns a color object using the specified opacity and RGB component values in the Display P3 color space.

you can use displayP3Red instead of colorLiteralRed

let startingColorOfGradient = UIColor(displayP3Red: 255/255, green: 255/255, blue: 255/255, alpha: 1.0).cgColor
let endingColorOFGradient = UIColor(displayP3Red: 251/255, green: 247/255, blue: 234/255, alpha: 1.0).cgColor
let gradient: CAGradientLayer = CAGradientLayer()
Related