NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

How to dynamically adjust the color of an SF Symbol

Paul Hudson    @twostraws   

Updated for Xcode 14.2

New in iOS 16

Some SF Symbols support variable coloring, which means they can have different parts filled based on a fraction between 0 and 1.

For example, this shows a Wi-Fi icon that is partly filled in:

Image(systemName: "wifi", variableValue: 0.5)

Download this as an Xcode project

This value can change over time based on whatever state you’re using in your code. For example, we could use a slider to change various icons according to a local property:

struct ContentView: View {
    @State private var value = 0.0

    var body: some View {
        VStack {
            HStack {
                Image(systemName: "aqi.low", variableValue: value)
                Image(systemName: "wifi", variableValue: value)
                Image(systemName: "", variableValue: value)
                Image(systemName: "waveform", variableValue: value)

            Slider(value: $value)
        .font(.system(size: 72))

Download this as an Xcode project

Hacking with Swift is sponsored by Waldo

SPONSORED Thorough mobile testing hasn’t been efficient testing. With Waldo Sessions, it can be! Test early, test often, test directly in your browser and share the replay with your team.

Try for free today!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 3.7/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.