TechnologiesSwiftUIPickers and Text Fields

MenuOrder struct

iOSmacOStvOSwatchOSvisionOSiOS 16.0+✓ renders

The order in which a menu presents its content.

How it works

MenuOrder is a structure whose constants tell SwiftUI how to sequence the items inside a menu — specifically, whether they appear in the order you declared them or in a system-adapted order optimized for the menu's position on screen. By default a menu may flip its contents so the items nearest the user's touch are easiest to reach, which can place your last button at the top. Reach for MenuOrder when that automatic reordering works against you and you need the items to read in a deliberate, stable sequence.

  1. Choose a presentation order with MenuOrder's constants

    MenuOrder exposes the values you pass to control sequencing. .automatic lets the system decide the order based on context, while .fixed pins items to the exact order you wrote them in the menu's content closure. The example selects .fixed so the flavor buttons stay anchored top-to-bottom.

  2. Apply it with the menuOrder(_:) modifier

    You don't construct a MenuOrder yourself at the call site — you hand one of its constants to the menuOrder(_:) view modifier, which sets the order for menus in that part of the view hierarchy. Here .menuOrder(.fixed) is attached after the Menu so its contents are presented in declaration order.

  3. Scope it to the menu it should affect

    Because menuOrder(_:) flows down through the environment, placing it on a Menu governs that menu and any menus nested within it. In the example it sits directly on the Menu("Pick a Flavor: ..."), so the Vanilla, Chocolate, and Strawberry buttons all inherit the fixed ordering.

  4. Rely on Equatable and Hashable conformance

    MenuOrder conforms to Equatable and Hashable, so its constants compare cleanly and can be stored in state or driven by a binding. That lets you swap between .fixed and .automatic from a value rather than branching on view structure.

Try it — Change .menuOrder(.fixed) to .menuOrder(.automatic) and open the menu near the bottom of the screen to watch the system reverse the Vanilla/Chocolate/Strawberry order so the nearest item lands first.

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.

MenuOrder.swift
struct MenuOrderDemo: View {
    @State private var flavor = "Vanilla"
    var body: some View {
        Menu("Pick a Flavor: \(flavor)") {
            Button("Vanilla") { flavor = "Vanilla" }
            Button("Chocolate") { flavor = "Chocolate" }
            Button("Strawberry") { flavor = "Strawberry" }
        }
        .menuOrder(.fixed)
        .padding()
    }
}
Live preview
Pick a Flavor: Vanilla
Pick a Flavor: Vanilla
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →