Tim Smith
Tim Smith

Reputation: 63

Gradient Not showing swift Xcode

I am fairly new to Swift and XCode. I have just learnt how to create a gradient on a Navigation Controller from a tutorial video, however in the video I watch the guy used a UIColor and I want to use a CGColor, so I used this code:

class func gradientLayerForBounds(bounds: CGRect) -> CAGradientLayer {
  let layer = CAGradientLayer()
  let topColor = UIColor(red: 244, green: 45, blue: 78, alpha: 100)
  let bottomColor = UIColor(red: 245, green: 67, blue: 58, alpha: 100)

  layer.bounds = bounds
  layer.colors = [topColor.CGColor, bottomColor.CGColor]

  return layer
}

However when I build and run the project, nothing appears, I get no error or warning the Debug box. Hope someone can help me out!

Upvotes: 1

Views: 1061

Answers (1)

Jordan
Jordan

Reputation: 424

When using custom colors you need to divide the color by 255 e.g. UIColor(red: 244/255, green: 45/255, blue: 78/255, alpha: 100).

It appears you haven't done that, so I would recommend trying:

let topColor = UIColor(red: 244/255, green: 45/255, blue: 78/255, alpha: 100)
let bottomColor = UIColor(red: 245/255, green: 67/255, blue: 58/255, alpha: 100)

That should solve your problem.

Upvotes: 1

Related Questions