How it works
DefaultGaugeStyle is the gauge style SwiftUI applies automatically when you create a Gauge without specifying a style of your own. It conforms to GaugeStyle and renders a gauge in the form best suited to the current platform and context, so the appearance stays consistent with the system and adapts as the environment changes. Reach for it when you want the standard gauge presentation, or when you need to override an inherited style further down a view hierarchy and restore the system default.
Get the default by creating a Gauge
Every
Gaugeresolves to aGaugeStyle, and in the absence of any other style that resolution isDefaultGaugeStyle. TheGauge(value:)here, fed a normalized value of0.65, is already drawn by this style even before any modifier is attached.Apply it explicitly with gaugeStyle(_:)
The
gaugeStyle(_:)modifier sets the style for a gauge and everything beneath it in the hierarchy. PassingDefaultGaugeStyle()names the system default outright, which is useful to reset to the standard look inside a subtree that an ancestor styled differently. In the example,.gaugeStyle(DefaultGaugeStyle())makes that choice visible.Initialize with DefaultGaugeStyle()
DefaultGaugeStylehas a single no-argument initializer; the style itself carries no configuration, because it defers all appearance decisions to the platform. You writeDefaultGaugeStyle()and let the system pick the concrete rendering.Feed it the gauge's labels
The style lays out the labels you supply to the
Gauge. The primary label here,Text("Speed"), identifies the gauge, and thecurrentValueLabel,Text("65%"), describes its present reading; both are positioned and styled byDefaultGaugeStylerather than by you.Let it adapt to context
Because it is the default, this style honors the surrounding environment such as accent color, size class, and platform conventions without extra code. Wrapping the gauge in
.padding()only insets it; the gauge's own form remains whateverDefaultGaugeStyledeems appropriate there.
Gauge(value: 0.65) to Gauge(value: 0.1) (or 0.95) and watch how DefaultGaugeStyle redraws the filled portion to reflect the new 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 DefaultGaugeStyleDemo: View {
var body: some View {
Gauge(value: 0.65) {
Text("Speed")
} currentValueLabel: {
Text("65%")
}
.gaugeStyle(DefaultGaugeStyle())
.padding()
}
}