TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Question about a Custom Color Picker

Forums > SwiftUI

Hi everyone. New to SwiftUI and still trying to wrap my head around everything.

I'm looking at creating a Custom Color Picker for my app and I am being shown an error in the Preview that i just dont know how to fix.

It mentions a missing argument for parameter "selectedColor" in call

I know why it is bringing up the error but I've tried many ways to fix the issue and they seem to bring up other issues.

Does anyone know what is missing in the code below?

import SwiftUI

struct CustomColorPicker: View {
    @Binding var selectedColor: Color
    let colors: [Color] = [.red,
                           .orange,
                           .yellow,
                           .green,
                           .cyan,
                           .blue,
                           .purple,
                           .indigo,
                           .pink,
                           .teal,
                           .brown]

    var body: some View {
        ScrollView(.horizontal, showsIndicators: false) {
            HStack(spacing: 20) {
                ForEach(colors, id: \.self) { color in
                    Button {
                        selectedColor = color
                    } label: {
                        Circle()
                            .fill(color)
                            .frame(width: 50, height: 50)
                            .overlay(
                                Circle()
                                    .stroke(Color.white, lineWidth: self.selectedColor == color ? 3 : 0)
                            )
                    }
                }
            }
        }
    }
}

struct CustomColorPicker_Previews: PreviewProvider {

    static var previews: some View {
        CustomColorPicker()
    }
}

2      

The preview needs to know which color to display so you can try to pass a constant value only for display purposes.

struct CustomColorPicker_Previews: PreviewProvider {

    static var previews: some View {
        CustomColorPicker(selectedColor: .constant(.red))
    }
}

2      

Yes. That's exactly what it needed.

I was aware that the Preview needed a value to display but I had tried to add in .red directly as a parameter to no avail.

I'd also tried to input $selectedColor as well but that also didn't work.

Thanks so much for the help

2      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

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

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.