skip to content

In SwiftUI, what mirrors automatically when the layout direction is right-to-left, and when does an image need flipsForRightToLeftLayoutDirection?

level: middleimportance: should knowfreq 55%

answer

  1. positions flip, contents do not
  2. leading and trailing, never left and right
  3. layoutDirection comes from the user's language
  4. opt in for directional artwork

basics

~20 s

In right-to-left, SwiftUI flips each view's x position, so stack order, leading and trailing alignment, padding and edges mirror automatically. Image contents are not mirrored: directional artwork opts in with flipsForRightToLeftLayoutDirection(true), while logos and photos stay unchanged.

solid answer

~40 s

The system sets the `layoutDirection` environment value from the user's language, and in `.rightToLeft` SwiftUI flips the x position of each view within its parent. So an `HStack` starts at the right, `.leading` alignment and `padding(.leading, 16)` resolve to the right edge, and system controls such as the back button follow. What does not flip is content: an image keeps its pixels and text keeps its glyphs. A custom back or next arrow therefore needs `flipsForRightToLeftLayoutDirection(true)`, or iOS 17's `layoutDirectionBehavior(.mirrors)`; a logo, a photo or a checkmark must not flip. To check, preview with `.environment(\.layoutDirection, .rightToLeft)`, because overriding only the locale does not change the direction.

code

swift · 22 lines
swift
import SwiftUI

struct BookingRow: View {
    let hotel: String

    var body: some View {
        HStack {
            Image("hotel-logo")                      // a logo: never mirrored
            Text(hotel)
                .frame(maxWidth: .infinity, alignment: .leading)
            Image("arrow-next")                      // custom directional artwork
                .flipsForRightToLeftLayoutDirection(true)
        }
        .padding(.leading, 16)                       // leading: right edge in Arabic
    }
}

#Preview("Arabic, right-to-left") {
    BookingRow(hotel: "Dar Al Bahar")
        .environment(\.locale, Locale(identifier: "ar"))
        .environment(\.layoutDirection, .rightToLeft)
}

go deeper

for a junior

Recall that leading and trailing follow the reading direction and that SwiftUI flips layout for Arabic without code changes.

for a middle

Explain that SwiftUI flips view positions but not contents, so directional images opt in with flipsForRightToLeftLayoutDirection or layoutDirectionBehavior.

for a senior

Classify a real screen: which images must mirror, which must not, which regions stay left-to-right, and how previews prove it.

for a principal

Set the team rule that layout uses leading and trailing only and that every release gets a right-to-left pass with an owner.

## Where the direction comes from SwiftUI keeps the current direction in the environment value **`layoutDirection`**, of type `LayoutDirection` with the cases `.leftToRight` and `.rightToLeft`. The system sets it from the user's language and region, so an Arabic user's app starts in `.rightToLeft` without any code. Views can read it with `@Environment(\.layoutDirection)` and a subtree can override it with `.environment(\.layoutDirection, .rightToLeft)`. Apple's documentation is explicit that most views need nothing more: in a right-to-left environment the origin of the coordinate space moves to the upper-right corner, positive x points left, and **SwiftUI horizontally flips the x position of each view within its parent**. Layout code written in terms of leading and trailing produces the right result in both directions. ## What mirrors on its own - **Stack order.** An `HStack` places its first child at the leading edge, which is the right edge in Arabic. - **Leading and trailing.** `HorizontalAlignment.leading`, `.frame(alignment: .leading)`, `padding(.leading, 16)` and `Edge.leading` all resolve to the right side in right-to-left. Custom alignment guides reverse too. - **System controls.** Navigation bars, lists, swipe actions and the system back button follow the environment direction. - **Text alignment.** `multilineTextAlignment(.leading)` aligns to the right in right-to-left. SwiftUI exposes leading and trailing, not left and right, in its alignment and edge types, so code that sticks to them mirrors by construction. ## What does not mirror The flip moves views; it does not redraw their **contents**: - **Images** keep their pixels. A custom back arrow drawn pointing left still points left in Arabic, now sitting on the right edge and pointing into the screen. - **Text glyphs** are not mirrored by the layout flip; each script is shaped in its own direction. - **Values you computed yourself**, such as a drawing that assumes the origin is on the left, follow your math, not the environment. For **directional artwork** you opt in with `flipsForRightToLeftLayoutDirection(true)`, available since iOS 13, which mirrors the view's contents horizontally when the direction is right-to-left. iOS 17 added the more general `layoutDirectionBehavior(_:)`, taking `.fixed`, `.mirrors` or `.mirrors(in:)`. SF Symbols names directional symbols by direction of travel, such as `chevron.backward`, which Apple's documentation uses for a Back control; prefer those names over a fixed side such as `chevron.left`, and confirm the rendering in a right-to-left preview. ## Choosing what to flip | Element in a booking screen | Flip in right-to-left? | Why | |---|---|---| | Custom "next step" arrow | Yes | It shows direction of travel | | Custom back arrow | Yes | Back points toward the trailing edge in Arabic | | Hotel or brand logo | No | A mirrored logo is a different, wrong mark | | Photos and maps | No | Mirroring would falsify the content | | Clock, play symbol, checkmark | No | Their meaning does not depend on reading direction | When a whole region must keep left-to-right order, for example a phone number, a card number or a seat map laid out like the aircraft, set `.environment(\.layoutDirection, .leftToRight)` on that subtree instead of fighting each child. ## When code should read the direction Most screens never read `layoutDirection`, and reading it to reverse things the layout already reverses produces a double flip: the view ends up back on the wrong side. Reading it is justified when code makes a choice the layout system cannot see: - picking between two different assets, for example an illustration drawn separately for right-to-left readers; - passing the direction to code outside SwiftUI's layout, which cannot see the environment; - deciding which way a custom, hand-drawn indicator should read, when it is built from raw coordinates rather than leading and trailing. In each case, `@Environment(\.layoutDirection) private var direction` gives the current value, and a preview with the override exercises both branches. ## Checking it 1. Add a preview that sets `.environment(\.layoutDirection, .rightToLeft)`. `locale` and `layoutDirection` are separate environment values, so a preview that overrides only the locale is not a right-to-left check. 2. Run the app in Arabic, or with Xcode's right-to-left pseudolanguage, and walk every screen with directional artwork. 3. Look for anything positioned with explicit coordinates and confirm it was written against the environment direction. What interviewers probe is the line between layout and content: SwiftUI mirrors where views go, and the engineer decides which pictures must turn around.

  • How do you keep a phone number or seat map in left-to-right order inside an Arabic screen?
    Override the direction for that subtree: `.environment(\.layoutDirection, .leftToRight)`. Every view inside it then lays out left to right while the rest of the screen stays right-to-left. That is cleaner than reversing children by hand, and it documents the intent at the one place it applies.
  • What does layoutDirectionBehavior(_:) add over flipsForRightToLeftLayoutDirection(_:)?
    `flipsForRightToLeftLayoutDirection(_:)`, from iOS 13, takes a Boolean and mirrors contents in right-to-left. `layoutDirectionBehavior(_:)`, from iOS 17, takes a `LayoutDirectionBehavior`: `.fixed`, `.mirrors`, or `.mirrors(in:)` naming the direction in which to mirror, and `Shape` exposes the same property. For a simple directional image either works.

  • Every layout needs a separate right-to-left branch in code
  • SwiftUI mirrors images automatically in Arabic
  • padding(.leading) always means the left edge
  • Setting the locale to Arabic in a preview is enough to test mirroring
  • Logos and photos should be flipped along with the layout