Dauris Little

  • About
  • Dauris’s Portfolio
  • Blogging Lyf
  • Contact

Gradient Views w/Swift

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

Gradient Views w/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

 

Tagged under: CAGradientLayer, gradient, iOS, programming w/ swift, swift

What you can read next

Using let or var within Swift
Constructing an eCommerce
Building eCommerce Sites and Things To Keep In Mind
Android w/ Fragments

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.

Categories

Recent Posts

  • Did someone say Navigation to the rescue: Android Jetpack

    If you been around the block a few times with A...
  • Using let or var within Swift

    I know I know I know before you say anything I ...
  • Show/Hide On-Screen Keyboard

    Android R is also known as Android 11 release b...
  • Using Enum w/Swift

    understand enum is nothing fancy and all langua...
  • Shimmering in Android

    You may or may not  have been aware of certain ...

© 2017. All rights reserved. Designed by Dauris Little

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