Dauris Little

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

Gradient Views w/Swift

Avatar photo
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

Show/Hide On-Screen Keyboard
Shimmering in Android
.NET C#
Identifiers & Keywords

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

  • Gesture Controls for Android w/Kotlin

    Incorporating Gesture controls in your android ...
  • Android Rating: In-App Review API

    An app rating and reviews are crucial if you wa...
  • QR Reader in Android w/ Kotlin

    Turn your phone's camera into a QR scanner...
  • Creating Advance Custom Snackbar w/ Kotlin

    Ask 100 different developers about what they fi...
  • Swift Has Tuple

    Swift provides us with a type called Tuple whic...

© 2017. All rights reserved. Designed by Dauris Little

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