GO FURTHER, FASTER: Try the Swift Career Accelerator 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()
    }
}

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      

Go further, faster with the Swift Career Accelerator.

GO FURTHER, FASTER Unleash your full potential as a Swift developer with the all-new Swift Career Accelerator: the most comprehensive, career-transforming learning resource ever created for iOS development. Whether you’re just starting out, looking to land your first job, or aiming to become a lead developer, this program offers everything you need to level up – from mastering Swift’s latest features to conquering interview questions and building robust portfolios.

Learn more here

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.