SSG logoSubscribe
Jese Leos

Gradients in SwiftUI

Gradients are an essential design element that should not be overlooked when designing attractive and appealing user interfaces in SwiftUI. SwiftUI Gradients offer a smooth color transition that can add depth and dimension, and a feeling of elegance to your app.

SwiftUI has four distinct gradient types: EllipticalGradient, AngularGradient, RadialGradient, and LinearGradient. A color gradient represented as an array of color stops, each having a parametric location value.

We'll explore the power of gradients in this tutorial, including how to easily integrate them into the UI design.

Gradient
gradient.swift

_33
import SwiftUI
_33
_33
struct ContentView: View {
_33
let gradient = Gradient(colors: [.red, .yellow])
_33
let gradient2 = Gradient(colors: [.red, .orange, .red])
_33
let gradient3 = Gradient(stops: [.init(color: .red, location: 0),
_33
.init(color: .orange, location: 0.2)])
_33
var body: some View {
_33
VStack(spacing: 0) {
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient2,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient3,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
}
_33
}
_33
}

Gradient is a struct that contains data for creating different kinds of gradients. It has two different initializer function, with Stops and Colors. When we initialize the gradient with Stops, we can decide distribution of colors(in a range 0 to 1). On the other hand, Colors object helps equally distribution.

LinearGradient
gradient.swift

_33
import SwiftUI
_33
_33
struct ContentView: View {
_33
let gradient = Gradient(colors: [.red, .yellow])
_33
let gradient2 = Gradient(colors: [.red, .orange, .red])
_33
let gradient3 = Gradient(stops: [.init(color: .red, location: 0),
_33
.init(color: .orange, location: 0.2)])
_33
var body: some View {
_33
VStack(spacing: 0) {
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient2,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient3,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
}
_33
}
_33
}

The linear gradient is the simplest gradient. You can set the beginning and ending points and a range of colors. It creates a gradient transition in a straight line between two or more colors. We can provide the positions thanks to UnitPoint struct (x,y cordinate space). SwiftUI has predefined UnitPoint: .zero, .top, .bottom, .leading, .trailing.

RadialGradient

RadialGradient creates a gradient transition between two or more colors in a circular pattern. Unlike the LinearGradient, a radial gradient begins by adding color concentrically from the center point and scales to fit within the specified radius. Concept is quite similar, we define start and end radius with center point instead of start and end points. center is UnitPoint, startRadius and endRadius are CGFloat objects.

gradient.swift
RadialGradient.swift

_38
struct RadialGradient: View {
_38
let gradient = Gradient(colors: [.red, .yellow])
_38
_38
var body: some View {
_38
HStack {
_38
VStack {
_38
Rectangle()
_38
.fill(
_38
RadialGradient(gradient: gradient, center: .center, startRadius: 1, endRadius: 99)
_38
)
_38
.frame(width: 250, height: 250)
_38
Text("radius 1 to 100")
_38
.fontWeight(.semibold)
_38
.foregroundColor(.black)
_38
}
_38
VStack {
_38
Rectangle()
_38
.fill(
_38
RadialGradient(gradient: gradient, center: .center, startRadius: 1, endRadius: 99)
_38
)
_38
.frame(width: 250, height: 125)
_38
Text("radius 1 to 100")
_38
.fontWeight(.semibold)
_38
.foregroundColor(.black)
_38
}
_38
VStack {
_38
Rectangle()
_38
.fill(
_38
RadialGradient(gradient: gradient, center: .center, startRadius: 1, endRadius: 5)
_38
)
_38
.frame(width: 200, height: 100)
_38
Text("radius 1 to 50")
_38
.fontWeight(.semibold)
_38
.foregroundColor(.black)
_38
}
_38
}
_38
}
_38
}

AngularGradient

An angular gradient is also known as a conic gradient are the gradients where the color change is based on the angle. It cycling through various colors then returning to the beginning. We can initialize AngularGradient with a few different variables.

gradient.swift
RadialGradient.swift
AngularGradient.swift

_29
struct AngularGradient: View {
_29
let gradient = Gradient(colors: [.yellow, .orange, .red, .green, .blue, .mint])
_29
_29
var body: some View {
_29
HStack {
_29
VStack {
_29
Rectangle()
_29
.fill(
_29
AngularGradient(gradient: gradient, center: .center)
_29
)
_29
.frame(width: 200, height: 200)
_29
}
_29
VStack {
_29
Rectangle()
_29
.fill(
_29
AngularGradient(gradient: gradient, center: .center, angle: .degrees(90))
_29
)
_29
.frame(width: 200, height: 200)
_29
}
_29
VStack {
_29
Rectangle()
_29
.fill(
_29
AngularGradient(gradient: gradient, center: .center, startAngle: .degrees(90), endAngle: .degrees(180))
_29
)
_29
.frame(width: 200, height: 200)
_29
}
_29
}
_29
}
_29
}

EllipticalGradient

An angular gradient is also known as a “conic” gradient are the gradients where the color change is based on the angle. It cycling through various colors then returning to the beginning. We can initialize AngularGradient with a few different variables.

gradient.swift
RadialGradient.swift
AngularGradient.swift
EllipticalGradient

_23
struct EllipticalGradient: View {
_23
var body: some View {
_23
HStack {
_23
VStack {
_23
Text("EllipticalGradient")
_23
RoundedRectangle(cornerRadius: 20)
_23
.foregroundStyle(
_23
.ellipticalGradient(
_23
colors: [.yellow, .red]))
_23
.frame(width: 350, height: 150)
_23
}
_23
_23
VStack {
_23
Text("EllipticalGradient")
_23
RoundedRectangle(cornerRadius: 20)
_23
.foregroundStyle(
_23
.ellipticalGradient(
_23
colors: [.yellow, .red]))
_23
.frame(width: 100, height: 300)
_23
}
_23
}
_23
}
_23
}

Using of These Gradients

All of these gradients conform to the ShapeStyle and view protocols, therefore you can use them anywhere those two protocols will be expected. We can use them as a .background, .stroke and .fill.

gradient.swift
RadialGradient.swift
AngularGradient.swift
EllipticalGradient
StrokeBorder.swift

_23
struct ContentView: View {
_23
var body: some View {
_23
HStack(spacing: 12) {
_23
Circle()
_23
.strokeBorder(
_23
AngularGradient(gradient: Gradient(colors: [.red, .yellow, .green, .blue, .purple, .red]), center: .center, startAngle: .zero, endAngle: .degrees(360)),
_23
lineWidth: 50
_23
)
_23
.frame(width: 200, height: 200)
_23
Text("CodeWithSSG")
_23
.foregroundStyle(.white)
_23
.font(.bold(.largeTitle)())
_23
.background(
_23
LinearGradient(gradient: Gradient(colors: [.yellow, .red, .blue]), startPoint: .leading, endPoint: .trailing)
_23
)
_23
Circle()
_23
.fill(
_23
RadialGradient(gradient: Gradient(colors: [.orange, .yellow, .red, .blue, .green]), center: .center, startRadius: 50, endRadius: 100)
_23
)
_23
.frame(width: 200, height: 200)
_23
}
_23
}
_23
}

Gradient

Gradient is a struct that contains data for creating different kinds of gradients. It has two different initializer function, with Stops and Colors. When we initialize the gradient with Stops, we can decide distribution of colors(in a range 0 to 1). On the other hand, Colors object helps equally distribution.

LinearGradient

The linear gradient is the simplest gradient. You can set the beginning and ending points and a range of colors. It creates a gradient transition in a straight line between two or more colors. We can provide the positions thanks to UnitPoint struct (x,y cordinate space). SwiftUI has predefined UnitPoint: .zero, .top, .bottom, .leading, .trailing.

RadialGradient

RadialGradient creates a gradient transition between two or more colors in a circular pattern. Unlike the LinearGradient, a radial gradient begins by adding color concentrically from the center point and scales to fit within the specified radius. Concept is quite similar, we define start and end radius with center point instead of start and end points. center is UnitPoint, startRadius and endRadius are CGFloat objects.

AngularGradient

An angular gradient is also known as a conic gradient are the gradients where the color change is based on the angle. It cycling through various colors then returning to the beginning. We can initialize AngularGradient with a few different variables.

EllipticalGradient

An angular gradient is also known as a “conic” gradient are the gradients where the color change is based on the angle. It cycling through various colors then returning to the beginning. We can initialize AngularGradient with a few different variables.

Using of These Gradients

All of these gradients conform to the ShapeStyle and view protocols, therefore you can use them anywhere those two protocols will be expected. We can use them as a .background, .stroke and .fill.

gradient.swift

_33
import SwiftUI
_33
_33
struct ContentView: View {
_33
let gradient = Gradient(colors: [.red, .yellow])
_33
let gradient2 = Gradient(colors: [.red, .orange, .red])
_33
let gradient3 = Gradient(stops: [.init(color: .red, location: 0),
_33
.init(color: .orange, location: 0.2)])
_33
var body: some View {
_33
VStack(spacing: 0) {
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient2,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
Rectangle()
_33
.fill(
_33
LinearGradient(
_33
gradient: gradient3,
_33
startPoint: .leading,
_33
endPoint: .trailing)
_33
)
_33
}
_33
}
_33
}