KEEP CALM and build your own colors 🍭
I’ve been working with iOS for quiet a long time now and believe me, building a nice-looking visual interface is must for an app to get accepted by a larger audience. Good design, colors, concept everything matters when building a great UI.
Gradients is one of the things that adds life to the colors. Plain color or a gradient? Let’s see which one is more eye catching..
Ofcourse the second one. Isn’t it? Mixing 2 colors is much more appealing than a single color.
In this article, we’ll be discussing how to get that working in our own app. And trust me, it’s as easy as it seems to be. “Some more colors instead of a single one”. So, let’s begin then.
Core Animation Framework — Render, compose, and animate visual elements.
Every view in iOS has a layer associated with it that handles all the drawing and animations of the view. A view’s layer can be accessed using its
layer property, an object of
CALayer — An object that manages image-based content and allows you to perform animations on that content.
A layer’s main job is to manage the visual content that you provide but the layer itself has visual attributes that can be set, such as a background color, border, and shadow.
CAGradientLayer — A layer that draws a color gradient over its background color, filling the shape of the layer (including rounded corners).
You use a gradient layer to create a color gradient containing an arbitrary number of colors. By default, the colors are spread uniformly across the layer, but you can optionally specify locations for control over the color positions through the gradient.
Let’s see what
CAGradientLayer can provide us with.
colors— the colors that we need to make the gradient. It is an array of
CGColorRefobjects, i.e. you must provide
CGColorand not just an object of
startPoint— starting point of the gradient.
endPoint— end point of the gradient.
locations— An optional array of
NSNumberobjects defining the location of each gradient stop.
You can refer to the complete documentation here.
Well, now that we know what all is needed for a gradient, it’s just a matter of time now before we get it working.
Let’s first see how to get a gradient that we can use on a view’s
Great..!!! We have a gradient now. But, wait..!!! Why is it not working 🤔? refresh refresh refresh !!! noooo..still not working 🤯.
Aahhhh..!!! We forgot one little thing. Just having a gradient isn’t enough. We have to apply it to our desired view also. That’s so silly of us 🤦♀️. Well, here’s how we can apply the gradient to our view.
Here, the view can be any element be it a
UILabel etc. You can apply gradient to each one of them in the same way. Applying gradient to a
UICollectionViewCell is as simple as applying it to other elements. The
CAGradientLayer is added as the
sub-layer to the view’s layer. That’s so simple. Isn’t it?
That’s it. That’s all you we to get a nice-looking gradient on our screen.
Okay..!!! Now that we know how to get this working, I have a question on my mind. This is purely a UI thing, right?. Can we not get it in the
Storyboard itself 🧐? Though it not so much of code, but why write it everytime I need a gradient? Why not change some attributes in the storyboard’s
attribute inspector itself 🤔 ? It’ll be so easy if we can get that too.
Looks like we’re getting demanding? 😉 LOL. Well, I know the pain. Getting to change an attribute in the
storyboard itself is so much more satisfying than doing the same through code. Here we go then..🙂
@IBInspectable is what we can use to achieve this.
IBDesignable, it’s possible to build a custom interface for configuring your custom controls and have them rendered in real-time while designing your project.
You can read more about it here.
Set the custom class of view to
DesignableView in storyboard’s
Class Inspector. This is how it will appear in the
Attributes Inspector in storyboard.
Getting to see what it will look like is must for a great UI. Coding the same thing again and again to get the perfect look is much more tiring and hefty. This is one of the greatest advantage of storyboard, actually this is what storyboards are made for. Look and Feel of what will be the end result.
That’s it. Code once, use anywhere we want. No extra code, nothing. Add as many colors you want to show in the gradient. Go on, give it a try. 🤠
You can download the sample project from here.
Don’t forget to read my other articles:
Feel free to leave comments in case you have any doubts.
Create your free account to unlock your custom reading experience.