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

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

   

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

   

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

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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.