article

Friday, August 6, 2021

SwiftUI Color Picker Example

SwiftUI Color Picker Example

Color Picker shows the currently selected color and allow users to select a new color. In this example a Rectangle is filled with the chosen color.
ContentView.swift
 
//
//  ContentView.swift
//  swiftuidev
//
//  Created by Cairocoders
//

import SwiftUI

struct ContentView: View {
    
    @State private var selectedColor = Color.red
    
    var body: some View {
        
        NavigationView {
            VStack(spacing: 20) {

                Rectangle()
                    .fill(selectedColor)
                    .frame(width: 100, height: 100)

                ColorPicker("Set the rectangle color", selection: $selectedColor)
                    .padding()
                
                Spacer()
            
            }.navigationTitle("Color picker")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Related Post