An alternative to SwiftUI's Picker
that supports custom styles.
struct Item: Identifiable {
let id: Int
init(id: Int) {
self.id = id
}
}
struct ItemLabel: View {
private let itemId: String
init(item: Item) {
self.itemId = item.id
}
var body: some View {
Text("Cell - \(itemId)")
}
}
struct ItemPicker: View {
private let items: [Item] = 0..<100
@State private var selection: Item.ID = 0
var body: some View {
BetterPicker(items, selection: $selection, content: ItemLabel.init)
}
}
Currently, only Swift Package Manager is supported. Add it as a package via the URL:
https://github.com/MFB-Technologies-Inc/swiftui-pick-better.git
or
[email protected]:MFB-Technologies-Inc/swiftui-pick-better.git
The options displayed in a picker must conform to Identifiable
as that is how they are correlated to the current selection.
Selection values must conform to Hashable
. Where T
is the selection value for a given selectable item, the selection may be:
T
Optional<T>
K<T> where K: RandomAccessCollection, K.Element == T
Unlike SwiftUI's PickerStyle
, BetterPicker
supports custom styles. The BetterPickerStyle
protocol can be implemented to create reusable styles similar to SwiftUI's ButtonStyle
.
BetterPicker
currently includes one builtin style PlainInlineBetterPickerStyle
which is similar to the default iOS style of items in a vertical list.
BetterPicker
includes a resultBuilder
type StyleBuilder
for conditionally applying a style in a familiar way.
BetterPicker
does make use of view type erasure with AnyView
. This can have side-effects for various things including animations. However, no problems have been encountered so far.
BetterPicker
does not apply styles through a ViewModifier
. Instead, an instance function on the view is used. Therefore, a style can only be applied directly on the instance of a picker. This practice does not exactly align with how SwiftUI is intended to be used. There is potential for this to cause stability problems. However, no problems of this nature have been encountered so far.
- Add support for selection values that are
Equatable
withoutHashable
- Add support for options that are not
Identifiable
- Change to apply styles through a ViewModifier instead of instance function
The package includes an example app in the Example
directory with iOS, macOS, tvOS, and watchOS targets. Additionally, the example includes a more sophisticated style, GridBetterPickerStyle
.