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

Gradient Views w/Swift

by Dauris / Saturday, 19 January 2019 / Published in blog, iOS, Programming Languages, Swift

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

 

  • Tweet
Tagged under: CAGradientLayer, gradient, iOS, programming w/ swift, swift
Avatar photo

About Dauris

What you can read next

Constructing an eCommerce
Building eCommerce Sites and Things To Keep In Mind
Android w/ Fragments
Using Snackbar

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

All rights reserved. 

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