Docs
Inputs

Unit ToggleNew

A value with a glass unit toggle: the red pill flows to the unit you tap and the number rolls to the converted Measurement.

$npx swiftpieces add UnitToggle
Free · MIT + Commons ClauseiOS 17.0+Liquid Glassunitmeasurementconversiontogglehealthweatherglass
Type
UnitToggle
Files
UnitToggle.swift
Depends on
Nothing (Apple frameworks only)
Version
1.0.0

Colors and type for UnitToggle. .standard is the house palette.

Notes

  • Built on Foundation's Measurement, so any Dimension works: mass, temperature, length, speed, volume, energy. Pass the units to switch between, usually two.
  • Tapping the other unit converts the bound measurement with converted(to:) and writes it back in the new unit, so your model always holds what is shown.
  • The red pill flows to the chosen unit and the number rolls to the converted value with a numeric content transition. Unit symbols come from MeasurementFormatter, so they follow the locale.
  • fractionDigits caps the decimals shown. Turn showsValue off to put the toggle beside your own text.
  • VoiceOver reads the value with its full unit name, such as "72.4 kilograms", and each unit by name.
  • Under Reduce Motion the pill slides whole and the number changes without rolling.

Usage

UnitToggleExample()

Install

Pick one. Run it from the folder that contains your .xcodeproj and the files land inside your app.

Terminal

$npx swiftpieces add UnitToggle

Or ask your coding agent · Claude Code, Cursor or Xcode, once the MCP server is connected

>Add the SwiftPieces "Unit Toggle" piece to my app

Or copy the source above into your app.

New to SwiftUI?If you know React, CSS or Tailwind, SwiftUI is easier to pick up than it looks.Try real app patterns first

Building a whole app? See SwiftPieces Pro →

All inputs and forms →

Build it yourself: SwiftUI haptics guide

On this page

SwiftPiecesPro

Build the whole app with one library.

Production-ready screens, whole flows, complete app templates, a Build Kit your agent follows and Pro remixing.

Explore Pro