Skip to content

Latest commit

 

History

18 Commits

Folders and files

Repository files navigation

NumberPicker

version package manager, swift PM Apple Platforms, iOS 26+ swift 5 license MIT

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.

Three screenshots showing the number picker in action. The first image shows the keypad entry. The second image shows numeric keyboard entry. The third image shows the keypad entry with Voice Control labels.

Content

  1. Accessibility Support
  2. Requirements
  3. Installation
  4. Usage Examples
  5. Author
  6. License

Accessibility Support

  • VoiceOver
  • Voice Control
  • Larger Text
  • Dark Interface
  • Differentiate Without Color Alone
  • Sufficient Contrast
  • Reduced Motion

Note: May not be suitable for Assistive Access.

Requirements

  • iOS 26+
  • Swift 5+

Current Limitations

This version of the number picker is designed to work with positive integer values only.

Installation

Swift Package Manager

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"),
    ]
)

Manually

To manually add this library to your project, drag the Sources folder into the project tree.

Usage examples

NumberPicker

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
                }
            }
        }
    }
    
}

DigitPad and NumericKeyboard Views

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).


Number Picker Default

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 = .pad

Author

NumberPicker was created by David Kennedy.

License

NumberPicker is available under the MIT license. See the LICENSE file for more info.

Releases

Packages

Contributors

Languages