NumberPicker provides an accessibility-compatible SwiftUI control that lets the user enter a numeric value using either a keypad or the system keyboard.
The number picker will remember the user’s preferred entry method between uses.
Usage: A Stepper control is usually chosen for small, limited adjustments, but it is less suitable for values that may change by large or arbitrary amounts. NumberPicker can assist here, either alongside a Stepper or as a substitute when the user needs more free range over their input.
- VoiceOver
- Voice Control
- Larger Text
- Dark Interface
- Differentiate Without Color Alone
- Sufficient Contrast
- Reduced Motion
Note: May not be suitable for Assistive Access.
- iOS 26+
- Swift 5+
This version of the number picker is designed to work with positive integer values only.
You can use the Swift Package Manager to install NumberPicker by adding it to your Package.swift file:
import PackageDescription
let package = Package(
name: "NumberPicker",
targets: [],
dependencies: [
.Package(url: "https://github.com/zenopolis/NumberPicker", from: "0.2.1"),
]
)To manually add this library to your project, drag the Sources folder into the project tree.
Using the number picker to get an integer value.
The following code displays a button – that when pressed – shows the number picker with the user’s preferred entry method.
import SwiftUI
import NumberPicker
struct NumberPickerExample: View {
@Binding var value: Int
@State var newValue: Int? = nil
var body: some View {
HStack {
Text("Value: ")
NumberPicker(title: "Change Value", value: $newValue) {
Text("\(value)")
}
.onAppear {
// create a temporary copy of the value
newValue = value
}
.onChange(of: newValue) {
// validate the result
if let newValue {
value = newValue
}
}
}
}
}You can also access the views presented by the number picker directly.
The following example directly accesses the DigitPad view.
import SwiftUI
import NumberPicker
struct DigitPadExample: View {
@Binding var value: Int
@State var newValue: Int? = nil
var body: some View {
DigitPad(value: $newValue)
.onAppear {
// create a temporary copy of the value
newValue = value
}
.onChange(of: newValue) {
// validate the result
if let newValue {
value = newValue
}
}
}
}To use the NumericKeyboard view, replace DigitPad(value: $newValue) in the example with NumericKeyboard(value: $newValue).
NumberPicker stores the entry method, either .pad or .keyboard, the user last used when they invoked the number picker. When they next use the picker, the same entry method is presented.
The entry method value is stored in user defaults and can be accessed via the entryMethod static propery.
// Read default
let entryMethod = NumberPicker.entryMethod
// Set default
NumberPicker.entryMethod = .padNumberPicker was created by David Kennedy.
NumberPicker is available under the MIT license. See the LICENSE file for more info.
