SwiftUI onHingeChange - Hinge Angle Effects on iPhone Duo
iPhone Duo gives your app something no iPhone has offered before: a physical control that people move with both hands. Starting in iOS 27.1, SwiftUI reports the state of that hinge through the onHingeChange(isEnabled:_:) modifier, so you can respond as someone opens and closes the device.
It's tempting to treat this as a layout signal: read the angle, decide the device is folded, and rearrange your views. Apple's guidance points the other way. In the Leverage multiple displays and scenes on iPhone Duo tech talk, Apple frames hinge data as a live signal you react to, which suits interactions and effects. When the question is where content should go, the talk sends you to the arrangement and reserved region APIs.
This article shows how to use each API for the job it's designed for:
- Reach for hinge updates when people use the hinge to control something, like the brightness of a lamp, a game mechanic, or a live angle display.
- Use
ArrangementViewandReservedRegionwhen the fold changes where your content should go.
Note: The hinge APIs require iOS 27.1 or later. The SwiftUI types are available on every platform, but the hinge value is
nilon devices that don't have one, so always provide a fallback.
Meet the Hinge Types
The hinge API is small. It's made up of one modifier and three types.
DeviceHingeContext
DeviceHingeContext describes the hinges of the current device. Its only property, hinge, is an optional DeviceHinge. A nil value means there's no hinge to report.
DeviceHinge
DeviceHinge represents a single hinge and provides two values:
angle: The current angle of the hinge, as a SwiftUIAngle.status: A coarse description of the hinge position that the system works out for you, taking both the angle and the device orientation into account.
DeviceHinge.Status
DeviceHinge.Status has three values: .closed, .partiallyOpen, and .fullyOpen. If you only need to know which of these states the device is in, read status rather than interpreting the angle yourself. The system already accounts for orientation when it chooses a status, and your app stays consistent with the rest of the system.
Observe the Hinge
Attach onHingeChange to a view and store the values you need in state. According to Apple's documentation, SwiftUI runs the action right away with the starting context, then again each time the context changes.
In my testing with the iOS 27.2 simulator, that first call only reached views that were on screen when the app launched. A view that appeared later, while the hinge was still, received nothing until the hinge moved. Until you can confirm the behavior on a device, give your state a sensible starting value so the view looks right even if that first call never comes.
Before you build an effect, add a small readout to see the values the hinge actually reports. The documentation intentionally doesn't promise a specific update rate, angle range, or precision, so a readout is the most reliable way to calibrate an effect on a real device.
import SwiftUI
struct HingeReadout: View {
@State private var hinge: DeviceHinge?
var body: some View {
Group {
if let hinge {
VStack(alignment: .leading) {
Text("Angle: \(hinge.angle.degrees.formatted(.number.precision(.fractionLength(0))))°")
Text("Status: \(label(for: hinge.status))")
}
} else {
Text("No hinge")
}
}
.font(.caption.monospaced())
.padding(8)
.background(.thinMaterial, in: .rect(cornerRadius: 8))
.onHingeChange { _, newContext in
hinge = newContext.hinge
}
}
private func label(for status: DeviceHinge.Status) -> String {
switch status {
case .closed: "Closed"
case .partiallyOpen: "Partially open"
case .fullyOpen: "Fully open"
default: "Unknown"
}
}
}Add the readout as a debug overlay while you develop:
ContentView()
#if DEBUG
.overlay(alignment: .topLeading) {
HingeReadout()
.padding()
}
#endifTip:
DeviceHinge.Statusis a structure with static values, not an enumeration. Aswitchover it needs adefaultcase, even when you've handled all three documented values.
Drive an Effect with the Hinge Angle
The following example builds a desk lantern that glows brighter as someone opens the device. The hinge isn't deciding where anything goes. It's acting as a dimmer switch.
Start by separating the mapping logic from the view. A small, testable function turns a hinge into a glow level from 0 to 1:
enum LanternGlow {
/// The span of hinge angles, in degrees, that dims and brightens the lantern.
/// Confirm these values on a device using a hinge readout.
static let activeRange: ClosedRange<Double> = 20...160
static func level(for hinge: DeviceHinge?) -> Double {
// Without a hinge, show the lantern at full brightness.
guard let hinge else { return 1 }
switch hinge.status {
case .closed:
return 0
case .fullyOpen:
return 1
case .partiallyOpen:
return progress(for: hinge.angle)
default:
return 1
}
}
private static func progress(for angle: Angle) -> Double {
let lower = activeRange.lowerBound
let upper = activeRange.upperBound
let clamped = min(max(angle.degrees, lower), upper)
return (clamped - lower) / (upper - lower)
}
}Notice how status and angle work together. The status handles the two end states, so the lantern is fully off when closed and fully on when open, regardless of the exact angle. The angle only matters in between, where it provides the granular value the effect needs. Clamping keeps the result in range even if the reported angle falls outside the span you calibrated.
Next, use the glow level in the view:
struct LanternView: View {
/// The glow level, from 0 (off) to 1 (full brightness).
@State private var glow: Double = 0
@State private var hingeStatus: DeviceHinge.Status?
var body: some View {
ZStack {
Circle()
.fill(.orange.gradient)
.frame(width: 180, height: 180)
.blur(radius: 12 + 36 * glow)
.opacity(0.2 + 0.8 * glow)
Image(systemName: "lamp.table.fill")
.font(.system(size: 72))
}
.animation(.smooth(duration: 0.12), value: glow)
.sensoryFeedback(.selection, trigger: hingeStatus)
.onHingeChange { _, newContext in
hingeStatus = newContext.hinge?.status
glow = LanternGlow.level(for: newContext.hinge)
}
}
}Output:
The readout in the corner is the HingeReadout debug overlay from earlier. Notice the angle it reports doesn't exactly match the angle the hinge was set to, which is why calibrating with a readout matters.
This view uses the hinge in two ways:
- Continuous: The angle controls the blur and opacity of the glow. A short animation smooths the transition between updates, because the system decides how often updates arrive and how fine-grained they are.
- Discrete: Each status change plays a light haptic through
sensoryFeedback(_:trigger:). BecauseDeviceHinge.Statusconforms toEquatable, it works directly as a trigger.
Keep Hinge-Driven State Close to the Effect
Hinge updates can arrive many times per second while someone is moving the device. Each update that changes state causes SwiftUI to reevaluate the views that depend on it. Attach onHingeChange to the smallest view that uses the value, as LanternView does, rather than to a root view that owns your whole screen. Keep the action itself lightweight, too: assign a value and let SwiftUI handle the rendering.
Turn Updates Off When You Don't Need Them
Use the isEnabled parameter to stop hinge updates when your effect isn't active, such as when someone turns off a feature in settings:
struct LanternView: View {
var isLanternModeOn: Bool
@State private var glow: Double = 0
var body: some View {
LanternShape(glow: glow)
.onHingeChange(isEnabled: isLanternModeOn) { _, newContext in
glow = LanternGlow.level(for: newContext.hinge)
}
}
}Apple's documentation describes two extra calls around this parameter. Switching isEnabled to false triggers one final call with no hinge, as long as the previous context had one. Switching it back to true triggers a call with the current context, when there is one. With LanternGlow, that final hinge-less call would push the lantern to full brightness, the same as on a device without a hinge.
In the iOS 27.2 simulator, I didn't see either call. Turning updates off simply stopped them, and the lantern stayed at whatever glow it had. Turning them back on did nothing until the hinge moved.
Because the documented and observed behavior differ, don't build on either one. If the effect should look a certain way while it's off, set that state yourself. For example, reset the glow when the setting changes:
.onChange(of: isLanternModeOn) { _, isOn in
if !isOn { glow = 1 }
}This gives the same result whichever way the system behaves, and it makes the intent obvious to anyone reading the code. Handle a nil hinge deliberately, too. It's what devices without a hinge report, and LanternGlow treats it as full brightness so the lantern still looks right there.
Don't Lay Out Views Based on the Hinge
Now consider the pattern to avoid. The following recipe screen shows a step card on top of a full-screen photo. When the device is partially folded, it tries to move the card below the photo:
struct RecipeStepView: View {
@State private var isFolded = false
var body: some View {
Group {
if isFolded {
VStack(spacing: 0) {
StepPhoto()
StepCard()
}
} else {
ZStack(alignment: .bottom) {
StepPhoto()
StepCard()
}
}
}
// Avoid: hinge updates aren't a layout signal.
.onHingeChange { _, newContext in
isFolded = newContext.hinge?.status == .partiallyOpen
}
}
}Output:
In this recording, StepCard keeps a counter in @State to stand in for a running timer, and the dashed red band marks the fold. Watch the counter go from 2 back to 0 the moment the device folds, and the fold run straight through both the photo and the card.
The code compiles and seems to work at first, but it has several problems:
- It guesses the fold direction. A partially open device can be folded like a laptop, with the fold running horizontally, or like a book, with the fold running vertically. The
VStackonly suits one of them. In the other, the fold cuts through the middle of both views. - It doesn't know where the fold is. The hinge reports an angle and a status, not the frame or margins of the fold. The two halves of the
VStackare sized by their content, so the boundary between them is unlikely to line up with the fold. - It runs on a different clock than layout. The system streams hinge values continuously while someone bends the device, at whatever rate it chooses. Reserved regions change during layout instead, and the system may animate that change. Drive your layout from the hinge stream and your views can lag behind, or jump ahead of, the fold geometry on screen.
- It resets view state. The two branches of the
ifstatement have different identities. Each timeisFoldedchanges, SwiftUI discards theStepCardfrom one branch and creates a new one in the other, so any state inside it, such as a running kitchen timer, resets.
Use an Arrangement View Instead
The step card and photo have a clear foreground and background relationship, which is the case the overlay style of ArrangementView is designed for:
struct RecipeStepView: View {
var body: some View {
ArrangementView {
StepCard()
} secondary: {
StepPhoto()
}
.arrangementViewStyle(.overlay)
}
}Output:
With the device flat, the card (the primary view) floats over the photo (the secondary view). When the device is folded, it places the two views on opposite sides of the fold: the photo above the card in portrait orientation, and the photo on the leading side in landscape orientation. You don't track the hinge, choose a stack direction, or measure the fold. And because there's no if statement swapping containers, StepCard keeps its identity and state through every change.
Important: The arrangement view should wrap your content. Don't bury it inside a scroll view, list, grid, or navigation split view, and don't nest a tab view or split-view navigation inside it either.
If the two views are a main-and-detail pair that should never overlap, such as a recipe and its ingredient list, use .split instead of .overlay.
Read Reserved Regions for Custom Placement
Some content doesn't fit a two-view arrangement. A floating control that you position yourself, for example, might land directly in the fold. For these cases, read the fold's geometry with reservedRegions(kind:options:layoutDirectionBehavior:) on a GeometryProxy.
The following button sits centered near the bottom of the view. When a vertical fold is active, it moves to the center of the trailing pane, so it stays clear of the fold:
struct StartTimerButton: View {
private let bottomOffset: CGFloat = 60
var body: some View {
GeometryReader { proxy in
TimerButton()
.position(
x: horizontalCenter(in: proxy),
y: proxy.size.height - bottomOffset
)
}
}
private func horizontalCenter(in proxy: GeometryProxy) -> CGFloat {
let width = proxy.size.width
// An active division that's taller than it is wide runs vertically.
guard let fold = proxy.reservedRegions(kind: .division).first,
fold.frame.height > fold.frame.width else {
return width / 2
}
// Center the button in the space after the fold.
return (fold.frame.maxX + width) / 2
}
}Output:
Fully open: the button is centered at the bottom.
Partially folded: the button moves to the center of the trailing pane, clear of the fold (dashed red band).
A few details make this approach reliable:
- Margins are already included. A region's
framespans the fold and the breathing room the system keeps around it for touch targets, so there's nothing extra to add. - The fold only counts when the device is bent. Lay the device flat and the query comes back empty, so the button falls back to the center.
- Coordinates are local. Region frames are expressed in the space of the
GeometryProxyyou ask. Query them in the same view that places the button, and the numbers line up without any conversion. - Right-to-left mirroring is automatic. In a right-to-left layout, region frames flip along with your content. If your positioning math assumes left-to-right no matter the language, pass
.fixedforlayoutDirectionBehaviorso the frames stay as they are.
The move itself is intentionally small. The Human Interface Guidelines for iPhone Duo recommend shifting elements only as far as needed to keep them visible and easy to tap, because controls that jump across the screen are harder to find.
Tip: A region can report its frame before it becomes active. Pass
.includeInactivefor theoptionsparameter and checkisActiveon each result, and you can reserve space for the fold in advance instead of moving content the moment it appears.
Choose the Right API
When an Apple engineer was asked about hinge-aware layout on the Apple Developer Forums, the answer was a ranked list, from simplest to most custom. Start at the top and move down only when the option above doesn't fit:
- Use a standard system component, such as
NavigationSplitView. - Try
ArrangementView, which handles a primary and a secondary view for you. - Use reserved regions to position custom content, keeping the overall size of your views stable.
- Don't use
onHingeChangefor layout decisions.
The following table summarizes which API to use for common goals:
| Goal | API |
|---|---|
| Animate, tint, or scale content as the device opens | onHingeChange with angle |
| Play a sound or haptic when the device reaches a state | onHingeChange with status |
| Use the hinge as a game or instrument control | onHingeChange with angle |
| Show a list and detail side by side | NavigationSplitView |
| Place two related views on either side of the fold | ArrangementView |
| Keep a custom control or overlay out of the fold | reservedRegions(kind: .division) |
| Keep content clear of a camera | reservedRegions(kind: .occlusion) |
A quick test: if removing your hinge code would make the app less delightful, it's an effect, and onHingeChange is the right tool. If removing it would make content unreadable or hard to tap, it's layout, and it belongs to an arrangement or a reserved region.
Observe the Hinge in UIKit
UIKit apps use UIHingeInteraction, which follows the same guidance. Add the interaction to a view, and handle each update in its closure:
final class LanternViewController: UIViewController {
private let lanternView = LanternUIView()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(lanternView)
let hingeInteraction = UIHingeInteraction { [weak self] _, update in
guard let self else { return }
// A nil hinge means the view left a hierarchy that provides hinge updates.
guard let hinge = update.hinge else {
lanternView.glow = 1
return
}
let degrees = hinge.angle * 180 / .pi
lanternView.glow = glowLevel(forDegrees: degrees, status: hinge.status)
}
view.addInteraction(hingeInteraction)
}
private func glowLevel(forDegrees degrees: CGFloat, status: UIHinge.Status) -> CGFloat {
switch status {
case .closed: return 0
case .partiallyOpen: return min(max((degrees - 20) / 140, 0), 1)
default: return 1
}
}
}Note: In UIKit,
UIHinge.angleis aCGFloatmeasured in radians. In SwiftUI,DeviceHinge.angleis anAngle, which converts for you through itsdegreesandradiansproperties. If you share mapping code between the two frameworks, convert at the boundary so the calibrated range means the same thing in both. UIKit'sUIHinge.Statusis also an enumeration with a fourth case,.unknown, so give it a sensible default.
For layout in UIKit, use UIArrangementViewController or the reserved region APIs on UIView, just as you would use their SwiftUI counterparts.
Summary
The hinge on iPhone Duo is both a piece of hardware that divides the display and a control that people move. SwiftUI gives you separate APIs for each role:
- Use
onHingeChange(isEnabled:_:)to treat the hinge as input. Readstatusfor the closed, partially open, and fully open states, and readangleonly when an effect needs granular values. - Calibrate effects with a readout on a real device, clamp the values you map, and don't depend on a particular update rate or precision.
- Give hinge-driven state a sensible starting value, and set your "off" state yourself. Apple's documentation and the simulator disagree about the extra calls around appearing and
isEnabled, so write code that works either way. Handle anilhinge on purpose for devices without one. - Use
ArrangementViewfor two related views, andreservedRegions(kind:)for custom placement. Don't drive layout from hinge updates.
Keep the hinge in charge of how your app feels, and let the arrangement and region APIs decide how it's laid out. Your app then responds smoothly as someone opens it, and stays readable at every angle in between.
Thank you for reading. If you have any questions feel free to follow me on X and send me a DM. If this article helped you, Buy me a coffee.