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.
Choose a presentation order with MenuOrder's constants
MenuOrder exposes the values you pass to control sequencing.
.automaticlets the system decide the order based on context, while.fixedpins items to the exact order you wrote them in the menu's content closure. The example selects.fixedso the flavor buttons stay anchored top-to-bottom.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 theMenuso its contents are presented in declaration order.Scope it to the menu it should affect
Because menuOrder(_:) flows down through the environment, placing it on a
Menugoverns that menu and any menus nested within it. In the example it sits directly on theMenu("Pick a Flavor: ..."), so theVanilla,Chocolate, andStrawberrybuttons all inherit the fixed ordering.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
.fixedand.automaticfrom a value rather than branching on view structure.
.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.
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()
}
}