SPB Git

Repository Public MIT

doc-glass-segmented-control.md 2.6 KB

# GlassSegmentedControl

Identifier glass-segmented-control
Module SPGlassForms
Kind native-wrapper — the system segmented Picker, replaced by a menu Picker when the segments cannot be read
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Selection/GlassSegmentedControlExample.swift

# Minimal usage

swift
@State private var mode = "List"

GlassSegmentedControl("View", selection: $mode, options: ["List", "Grid", "Map"]) { $0 }

# Variants

Situation Presentation
Two to five options whose titles fit the width Segments
Titles wider than the available width Menu
More options than maximumSegments Menu
Accessibility text size Menu (segments cannot wrap, so titles would be cut)
Fewer than two options Menu

# Parameters

Parameter Type Default Meaning
title LocalizedStringKey — Name of the control: accessibility label, and label of the menu.
selection Binding<Option> — Selected option. Option is any Hashable type.
options [Option] — Options in order.
maximumSegments Int 5 Above this number of options the control is a menu.
title: (Option) -> String — Title of an option, already localised; a closure or a key path.

# States

The selected segment, pressed, focused and disabled (.disabled(_:)) are drawn by the system, including the Liquid Glass selection.

# Advanced example

swift
enum Period: String, CaseIterable, Hashable {
    case day, week, month, year
    var title: String { rawValue.capitalized }   // localise in a real application
}

GlassSegmentedControl("Period", selection: $period, options: Period.allCases, title: \.title)
    .onChange(of: period) { _, newValue in chart.load(newValue) }

The first part of the decision is the pure function GlassSegmentedLayout.presentation(optionCount:isAccessibilitySize:maximumSegments:); the width check is done by ViewThatFits.

# Accessibility

  • Both presentations are system pickers: label, selected value and keyboard navigation come from the system.
  • The title is always set, although segmented pickers do not display it.
  • At accessibility text sizes the menu keeps every title readable in full.

# Limits

  • Text titles only; no symbol-only segments.
  • 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.
  • The control takes its natural width; it does not stretch its segments to fill a wide container.
  • Single selection only.
Files main
Menu

Theme