LoginSignup
2

posted at

【SwiftUI】Menuのボタン順番について

はじめに

意識したことがないとあまり気づかないと思うのですが、
Menu内のボタンの表示される順番は、
Menulabelに近い方向から順番に表示されます。

スクリーンショット 2022-11-26 22.09.16.png スクリーンショット 2022-11-26 22.09.24.png

どこに配置されていても上から表示されてほしい場合があります。
そんな時の解決方法を紹介しようと思います。

解決方法

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            Menu {
                Button("ボタン1") { }
                Button("ボタン2") { }
                Button("ボタン3") { }
                Button("ボタン4") { }
                Button("ボタン5") { }
            } label: {
                Text("メニューボタン")
            }
+           .menuOrder(.fixed)
            Spacer()
            Menu {
                Button("ボタン1") { }
                Button("ボタン2") { }
                Button("ボタン3") { }
                Button("ボタン4") { }
                Button("ボタン5") { }
            } label: {
                Text("メニューボタン")
            }
+           .menuOrder(.fixed)
        }
    }
}
スクリーンショット 2022-11-26 22.12.25.png スクリーンショット 2022-11-26 22.12.15.png

おわり

iOS16からしか使えないです😭

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
2