Logo

dev-resources.site

for different kinds of informations.

Adjust the intensity/brightness of colors in SwiftUI views

Published at
12/26/2024
Categories
swift
swiftui
ios
ui
Author
divyesh vekariya
Categories
4 categories in total
swift
open
swiftui
open
ios
open
ui
open
Adjust the intensity/brightness of colors in SwiftUI views

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)
}

Image description

This creates a gradient where blue transitions to almost white.

Darken Colors

ForEach(0..<8) { num in
    Color.blue
        .brightness(Double(num) * -0.1)
}

Image description

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.

SwiftUI #Swift #iOSDev #ColorDesign #ProgrammingTips

Featured ones: