Is it possible to set a placeholder text for a Swift Picker? I've been trying to search for a helpful answer but I haven't been successful so far, so hopefully this will help in solving the issue:)
Currently when passing available list values to the Picker I also pass it a default value to start of with. However, this default value is treated the same as if the user picked it. What I'm trying to achieve is that the default value should be grayed out (like regular placeholders for standard textfields) and when the user opens the picker that value would 'dissapear' as default forcing the user to pick something from the list (but without losing the range) - so f.ex. if I have a picker for values between 1-200 and I set my placeholder to 100 the picker would Still show this value when you open it (to avoid scrolling from the beginning) but it wouldn't be directly taken as the target value unless the user actually selects it.
Is it possible to set a placeholder text for a Swift Picker? I've been trying to search for a helpful answer but I haven't been successful so far, so hopefully this will help in solving the issue:)
Currently when passing available list values to the Picker I also pass it a default value to start of with. However, this default value is treated the same as if the user picked it. What I'm trying to achieve is that the default value should be grayed out (like regular placeholders for standard textfields) and when the user opens the picker that value would 'dissapear' as default forcing the user to pick something from the list (but without losing the range) - so f.ex. if I have a picker for values between 1-200 and I set my placeholder to 100 the picker would Still show this value when you open it (to avoid scrolling from the beginning) but it wouldn't be directly taken as the target value unless the user actually selects it.
A Menu
may be a better option than a Picker
in this instance.
Menu("Label") {
Button("Menu Item", action: menuAction)
Button("Other Menu Item", action: otherMenuAction)
}
This can be done with a proxy
variable. It would separate the default value from the user's selection.
import SwiftUI
struct OptionalParentView: View {
//Initialize as nil since the user hasn't selected anything
@State var item: Int? = nil
var body: some View {
VStack{
//Shows the user's current selection - Sample
Text("\(item?.description ?? "not selected")")
OptionalPickerView(item: $item)
}
}
}
struct OptionalPickerView: View {
//User's value
@Binding var item: Int?
//Default value
let defaultValue: Int = 100
///Proxy between the Picker and the user's value
private var proxy: Binding<Int>{
Binding {
//Return the user's value or the default
item ?? defaultValue
} set: { newValue in
//Pass the selected value to the Binding
item = newValue
}
}
///Identifies if the user has selected a value
private var userHasSelected: Bool{
item == nil
}
var body: some View {
Picker("Selected item \(item?.description ?? "N/A")", selection: proxy){
ForEach(1...200, id:\.self){ item in
Text(item.description).tag(item as Int)
//Picker will be gray to start and blue once the user has selected something.
.foregroundColor(userHasSelected ? .gray : .blue)
}
}.pickerStyle(.wheel)
}
}
struct OptionalPicker_Previews: PreviewProvider {
static var previews: some View {
OptionalParentView()
}
}
Before selection
After selection
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.