SPB Git

Repository Public MIT

doc-glass-segmented-control.md 2.6 KB 69 lines markdown

1# GlassSegmentedControl23| | |4|---|---|5| Identifier | `glass-segmented-control` |6| Module | `SPGlassForms` |7| Kind | `native-wrapper` — the system segmented `Picker`, replaced by a menu `Picker` when the segments cannot be read |8| Platforms | iOS 26, iPadOS 26, macOS 26 |9| Example | `Examples/Gallery/Selection/GlassSegmentedControlExample.swift` |1011## Minimal usage1213```swift14@State private var mode = "List"1516GlassSegmentedControl("View", selection: $mode, options: ["List", "Grid", "Map"]) { $0 }17```1819## Variants2021| Situation | Presentation |22|---|---|23| Two to five options whose titles fit the width | Segments |24| Titles wider than the available width | Menu |25| More options than `maximumSegments` | Menu |26| Accessibility text size | Menu (segments cannot wrap, so titles would be cut) |27| Fewer than two options | Menu |2829## Parameters3031| Parameter | Type | Default | Meaning |32|---|---|---|---|33| `title` | `LocalizedStringKey` | — | Name of the control: accessibility label, and label of the menu. |34| `selection` | `Binding<Option>` | — | Selected option. `Option` is any `Hashable` type. |35| `options` | `[Option]` | — | Options in order. |36| `maximumSegments` | `Int` | `5` | Above this number of options the control is a menu. |37| `title:` | `(Option) -> String` | — | Title of an option, already localised; a closure or a key path. |3839## States4041The selected segment, pressed, focused and disabled (`.disabled(_:)`) are drawn by the system, including the Liquid Glass selection.4243## Advanced example4445```swift46enum Period: String, CaseIterable, Hashable {47    case day, week, month, year48    var title: String { rawValue.capitalized }   // localise in a real application49}5051GlassSegmentedControl("Period", selection: $period, options: Period.allCases, title: \.title)52    .onChange(of: period) { _, newValue in chart.load(newValue) }53```5455The first part of the decision is the pure function `GlassSegmentedLayout.presentation(optionCount:isAccessibilitySize:maximumSegments:)`; the width check is done by `ViewThatFits`.5657## Accessibility5859- Both presentations are system pickers: label, selected value and keyboard navigation come from the system.60- The title is always set, although segmented pickers do not display it.61- At accessibility text sizes the menu keeps every title readable in full.6263## Limits6465- Text titles only; no symbol-only segments.66- The width check measures the segments at their natural size; whether the switch happens exactly when a title would start to truncate was not verified on a running device.67- The control takes its natural width; it does not stretch its segments to fill a wide container.68- Single selection only.69
Files main
Menu

Theme