Skip to content

Instantly share code, notes, and snippets.

@atrinh0
Created August 6, 2022 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atrinh0/3df23140ba39df05692befb7153c8285 to your computer and use it in GitHub Desktop.
Save atrinh0/3df23140ba39df05692befb7153c8285 to your computer and use it in GitHub Desktop.
SwiftUI menu picker selection issue
import SwiftUI
struct ContentView: View {
@State private var selection: SortOrder = .ascending
var body: some View {
NavigationView {
VStack {
Text("Selected Sort Order:")
Text(selection.rawValue)
.font(.largeTitle)
Picker(selection: $selection) {
ForEach(SortOrder.allCases) {
Text($0.rawValue)
.tag($0)
}
} label: { }
.pickerStyle(.segmented)
.padding()
}
.toolbar {
Menu {
Picker(selection: $selection) {
ForEach(SortOrder.allCases) {
Text($0.rawValue)
.tag($0)
}
} label: { }
} label: {
Text("Sort by")
}
}
}
}
}
enum SortOrder: String, CaseIterable, Identifiable {
case ascending = "Ascending"
case descending = "Descending"
case high = "High"
case low = "Low"
var id: String { self.rawValue }
}
@atrinh0
Copy link
Author

atrinh0 commented Aug 6, 2022

The selection of a picker within the toolbar does not update to show the selected value.

This is working on Xcode 13.4.1, and from what I recall, it was also working on Xcode 14 beta 3.

Xcode 13.4.1 Xcode 14 beta 4
Screenshot 2022-08-06 at 17 46 32 Screenshot 2022-08-06 at 17 45 44

@atrinh0
Copy link
Author

atrinh0 commented Aug 6, 2022

This video shows selecting the different values in the menu picker within the toolbar updates the selection as shown in the middle of the screen, however the picker menu selection does not change. (The tick is still selecting the first item)

Simulator.Screen.Recording.-.iPhone.13.Pro.Max.-.2022-08-06.at.17.47.47.mp4

@atrinh0
Copy link
Author

atrinh0 commented Aug 6, 2022

Raised FB11104547

@atrinh0
Copy link
Author

atrinh0 commented Aug 13, 2022

Issue still exists in Xcode 14 beta 5

@atrinh0
Copy link
Author

atrinh0 commented Aug 16, 2022

A workaround, https://twitter.com/DanielKasaj/status/1559453531760992257

Setting the .id(UUID()) on the menu fixes the selection issue.

Menu {
    Picker(selection: $selection) {
        ForEach(SortOrder.allCases) {
            Text($0.rawValue)
                .tag($0)
        }
    } label: { }
} label: {
    Text("Sort by")
}
.id(UUID())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment