Dauris Little
  • About
  • Blogging Lyf
  • Contact
  • Portfolio
“Never be limited by other people’s limited imaginations.” – Dr. Mae Jamison

Gradient Views w/Swift

Saturday, 19 January 2019 by Dauris

When you want to give the background color to the view, but you want to add more colors and get the gradient effect, you need to add a layer to view,
which we are going to create, in few lines of code.

  • Create a CAGradientLayer
var gradientLayer: CAGradientLayer = {
   let gradientLayer =CAGradientLayer()
   gradientLayer.colors = [#Color1, #Color2] //note: colors you want to use
   gradientLayer.startPoint = CGPoint(x:0 y:0)
   gradientLayer.endPoint = CGPoint(x:1 y:1)
   gradientLayer.frame= CGRect.zero
   return gradientLayer
} ()

 

  • Add gradientLayer to the view layer
currentView.layer.addSublayer(gradientLayer)

 

  • Set frame of the gradientLayer
gradientLayer.frame = currentView.bounds

 

CAGradientLayergradientiOSprogramming w/ swiftswift
Read more
  • Published in blog, iOS, Programming Languages, Swift
No Comments

All rights reserved. 

TOP
This site uses tracking cookies to personalize content and ads. AcceptLearn More