How it works
SpringLoadingBehavior describes whether a control activates when a drag hovers over it long enough — the "spring-loading" gesture that lets someone open a folder, switch a tab, or trigger a button mid-drag without dropping what they're carrying. SwiftUI exposes the behavior as a small value type with named cases that you attach to a view rather than wiring up timers or hover tracking yourself. Reach for it when a control should respond during a drag-and-drop interaction, or when you want to explicitly opt a control in or out of that automatic activation.
Choose a behavior case
SpringLoadingBehavioris a struct that vends a handful of static values describing how the control reacts to a hovering drag. The example uses.enabledto say the control should spring-activate; the type also offers.automatic(defer to the system default for the control kind) and.disabled(never spring-load), letting you state intent precisely.Apply it with springLoadingBehavior(_:)
You install the behavior by calling the
.springLoadingBehavior(_:)view modifier, passing the case you chose. In the example,.springLoadingBehavior(.enabled)is attached to theButton, marking that button as a valid spring-loading target during a drag.Attach it to an activatable control
Spring loading is meaningful on controls that perform an action or change selection, so the modifier sits on the
Buttonhere — theLabel("Documents", systemImage: "folder")content is exactly the kind of destination a user might want to trigger while dragging files toward it. The behavior plugs into the control; the surroundingVStackand captionTextare just there to frame it.Let it cascade through the environment
The modifier writes into the environment, so applying
.springLoadingBehaviorto a container propagates the value to the controls inside it, while a more deeply nested modifier overrides it. Setting it directly on theButton, as in the example, scopes the.enabledvalue to just that control.
.springLoadingBehavior(.enabled) to .springLoadingBehavior(.disabled) and drag over the button — it will no longer spring-activate, isolating exactly what the behavior controls.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 SpringLoadingBehaviorDemo: View {
var body: some View {
VStack(spacing: 20) {
Text("Spring Loading")
.font(.headline)
Button {
} label: {
Label("Documents", systemImage: "folder")
}
.buttonStyle(.borderedProminent)
.springLoadingBehavior(.enabled)
Text("Hover a drag over this button to spring-activate it.")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.padding()
}
}