A simple picker to pick a enum.
import SwiftUI | |
struct EnumPicker<T: Hashable & CaseIterable, V: View>: View { | |
@Binding var selected: T | |
var title: String? = nil | |
let mapping: (T) -> V | |
var body: some View { | |
Picker(selection: $selected, label: Text(title ?? "")) { | |
ForEach(Array(T.allCases), id: \.self) { | |
mapping($0).tag($0) | |
} | |
} | |
} | |
} | |
extension EnumPicker where T: RawRepresentable, T.RawValue == String, V == Text { | |
init(selected: Binding<T>, title: String? = nil) { | |
self.init(selected: selected, title: title) { | |
Text($0.rawValue) | |
} | |
} | |
} |
This comment has been minimized.
This comment has been minimized.
It works great. But in most cases, the raw values of the enums are not good enough to be displayed to the user and in some cases, we have Int as raw values. So I wrote another extension.
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
It is really simple to use if you have a case iterable enum with a raw value of String:
Just simply:
As it will produce a
Text
-view itself; but if your enum does not have such string as a raw value, you can return a view in a closure:and you can of course also change the picker style, just add it to the environment: (Thanks @tobiasdm)
PS: If you rely on a custom view instead of the string value, remember that
SegmentedPickerStyle
can only show one Image or one Text-view per segment.