SwiftUI makes it incredibly easy to tweak colors with the brightness(_:)
modifier. You can lighten or darken any color dynamically by passing a Double value:
0 retains the original color.
1 makes the color fully white.
Negative values darken the color toward black.
Here are two examples:
Lighten Colors
ForEach(0..<8) { num in
Color.blue
.brightness(Double(num) * 0.1)
}
This creates a gradient where blue transitions to almost white.
Darken Colors
ForEach(0..<8) { num in
Color.blue
.brightness(Double(num) * -0.1)
}
This generates a gradient where blue transitions to almost black.
Want to bring SwiftUI into your UIKit apps or master advanced Swift programming?
📘 Check out Integrating SwiftUI into UIKit Apps for seamless adoption.
📘 Explore Swift Gems for expert tips and techniques.
Top comments (0)