I've been struggling with this issue and it's still exists (xcode 14 beta iOS 16).
I came up with this solution I hope it helps someone until Apple solve this
The label will not show if the selected item is not exist in the array items. So I'm adding the label in the array and the same label have to be the vale of the selected item.
this will cause the label showing all the time so when the user click to popup the list delete the label if you want.
@State private var items = ["please select an option","Option1","Option2","Option3","Option4","Option5"]
@State private var selectedItem = "please select an option"
var body: some View {
Picker(selection: $selectedItem, label: Text(selectedItem)) {
ForEach(items, id: \.self) {
Text($0)
}
}.onTapGesture(){
if items.contains("please select an option") {
items.remove(at: 0)
}
}
}
Same code here in case you have an array of your own object instead of Strings
Make sure to use Int { hashValue } as id of your object
struct myStruct: Hashable, Identifiable {
var id: Int { hashValue } //UUID is not going to work for this issue
let myValue:String
let myOtherValue:String
}
@State private var structItems = [myStruct(myValue: "0", myOtherValue: "please select an option"),
myStruct(myValue: "1", myOtherValue: "Option1"),
myStruct(myValue: "2", myOtherValue: "Option2"),
myStruct(myValue: "3", myOtherValue: "Option3"),
myStruct(myValue: "4", myOtherValue: "Option4"),
myStruct(myValue: "5", myOtherValue: "Option5")]
@State private var selectedStructItem = myStruct(myValue: "0", myOtherValue: "please select an option")
var body: some View {
Picker(selection: $selectedStructItem, label: Text(selectedStructItem.myOtherValue)) {
ForEach(structItems, id: \.id) { val in
Text(val.myOtherValue).tag(val)
}
}.onTapGesture(){
if structItems.contains(myStruct(myValue: "0", myOtherValue: "please select an option")) {
structItems.remove(at: 0)
}
}
}