Search code examples
swiftuipickerpickerview

SwiftUI - Can't add modifiers to Picker View within a form


I'm currently working on Project 4 of #hackingwithswift. I'm having the problem of not being able to change the style of my picker from within a form view.

Here's a little snippet of the code I'm working on. Please assume the rest of the code is correct. No matter what modifier I put in .pickerStyle(), I always get the error:

Type 'WheelPickerStyle.Type' cannot conform to 'PickerStyle'; only struct/enum/class types can conform to protocols

NavigationView{
        Form{
            Section(header: Text("When do you want to wake up?") .font(.headline)) {
                
                DatePicker("Please enter a time", selection: $wakeUp, displayedComponents: .hourAndMinute)
                    .datePickerStyle(WheelDatePickerStyle())
                    .labelsHidden()
            }
            
            Section(header: Text("Desired amount of sleep")
                .font(.headline)) {
                
                Stepper(value: $sleepAmount, in: 4...12, step: 0.25) {
                    Text("\(sleepAmount, specifier: "%g") hours")
                }
            }
            
            Section(header: Text("Daily coffee intake")
                .font(.headline)) {
                    
                    Picker("Number of coffees had in a day", selection: $coffeeAmount) {
                            Text("1")
                            Text("2")
                            Text("3")
                        }
                    .pickerStyle(WheelPickerStyle)
                    }

Can anyone help me out? Please explain what I'm doing wrong, I'm a beginner!

Thanks in advance!


Solution

  • In your code

    Picker("Number of coffees had in a day", selection: $coffeeAmount) {
                                Text("1")
                                Text("2")
                                Text("3")
                            }
                        .pickerStyle(WheelPickerStyle)
                        }
    

    You should use WheelPickerStyle() instead of WheelPickerStyle