SwiftUI - 无法将修饰符添加到表单内的选取器 View

标签 swiftui picker pickerview

我目前正在从事#hackingwithswift 的项目 4。我遇到的问题是无法从表单 View 中更改选择器的样式。

这是我正在处理的代码的一小段。请假设代码的其余部分是正确的。无论我在 .pickerStyle() 中添加什么修饰符,我总是会收到错误:

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

谁能帮帮我吗?请解释我做错了什么,我是初学者!

提前致谢!

最佳答案

在你的代码中

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

您应该使用WheelPickerStyle()而不是WheelPickerStyle

关于SwiftUI - 无法将修饰符添加到表单内的选取器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64620513/

相关文章:

ios - 选择器 View 数组行将只显示 0

ios - 有没有办法在 SwiftUI 中创建新的手势?

macos - 如何使用 SwiftUI 构建 AppKit TableView ?

android - Appcompact DialogFragment单选复选标记颜色

ios - UIDatePicker 在 iOS9 RTL 上翻转

ios - Swift - 无法使 PickerView 库正常工作

SWIFTUI - 即使使用了 clipped() 选择器 View 触摸/可滚动区域仍然超出框架

textfield - SwiftUI TextField 光标颜色

datetime - 如何向 Bootstrap DateTimePicker 添加分钟步长?

swift - 当 PickerView 第一次弹出/用户尚未开始任何访问时,如何从 PickerView 获取第一个值