How it works
A Gauge is a view that shows a value within a bounded range, giving people an at-a-glance reading of where something stands between a known minimum and maximum. Reach for it whenever a quantity has natural limits and the position within those limits matters more than the raw number alone — battery level, download progress, a temperature against safe bounds, or a fitness ring toward a goal. Unlike a progress indicator, a Gauge is built to display a steady measurement rather than the advancement of a task, and its appearance adapts to platforms and contexts ranging from full panels to compact accessory widgets.
Bind a value to a range with Gauge(value:in:)
The core initializer takes the current reading and the interval it lives in, then plots the value's relative position. Here
Gauge(value: progress, in: 0...1)reads from the@Statepropertyprogress(0.62) so the gauge fills to roughly 62% of its0...1range and updates automatically whenever that state changes.Name the measurement with the label closure
The trailing label describes what the gauge measures; some styles surface it, others use it for accessibility. The first closure supplies
Text("Battery"), identifying the quantity being shown so the value has meaning beyond the bare number.Surface readings with currentValueLabel, minimumValueLabel, and maximumValueLabel
These optional closures annotate the gauge with the value itself and the labels for each end of the range. The example pairs
currentValueLabel:showingText(progress, format: .percent)withminimumValueLabel:ofText("0")andmaximumValueLabel:ofText("100"), so the formatted percentage and the bounds appear alongside the indicator.Choose an appearance with gaugeStyle(_:)
The
gaugeStyle(_:)modifier swaps the rendering between linear bars, full dials, and compact accessory forms without changing the value logic. Applying.gaugeStyle(.accessoryCircular)draws a small circular ring suited to dense layouts and complications, where the value label sits in the center.Style the fill and spacing with standard view modifiers
Because
Gaugeis an ordinaryView, it composes with the usual modifier chain. The example applies.tint(.green)to color the active portion of the ring and.padding()to give it breathing room within its container.
@State private var progress = 0.62 to a value near the edge such as 0.05 and watch the ring and the .percent currentValueLabel both reflect the lower reading.Example & preview
Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.
struct GaugeDemo: View {
@State private var progress = 0.62
var body: some View {
Gauge(value: progress, in: 0...1) {
Text("Battery")
} currentValueLabel: {
Text(progress, format: .percent)
} minimumValueLabel: {
Text("0")
} maximumValueLabel: {
Text("100")
}
.gaugeStyle(.accessoryCircular)
.tint(.green)
.padding()
}
}