onHingeChange

Adds an action to perform when the hinge context of the view hierarchy changes.

View ModifieriOS 27.1+macOS 27.1+tvOS 27.1+watchOS 27.1+visionOS 27.1+onHingeChange(isEnabled:_:) in SwiftUI

Declaration

React
compose().onHingeChange({
  isEnabled?: boolean,
  action: (oldContext: SwiftUI.DeviceHingeContext, newContext: SwiftUI.DeviceHingeContext) => void,
})
Swift
nonisolated func onHingeChange(
    isEnabled: Bool = true,
    _ action: @escaping (DeviceHingeContext, DeviceHingeContext) -> Void
) -> some View

Parameters

Discussion

Use this modifier to be informed of changes to the hinge context of the device.

@State private var hinge: DeviceHinge? = nil

var body: some View {
    VStack {
        if let hinge {
            AngleDisplayView(angle: hinge.angle)
            StatusDisplayView(status: hinge.status)
        } else {
            ContentUnavailableView(
                "Hinge Unavailable",
                systemImage: "rectangle.split.2x1")
        }
    }
    .onHingeChange { _, newContext in
        hinge = newContext.hinge
    }
}

In the example above, the current angle and status of the hinge will be displayed in the app as you interact with the hinge.

See also

More in Events and lifecycle.