0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【SwiftUI】メニュー内のボタンを押した後、メニューを閉じない

Posted at

はじめに

iOS16.4からmenuActionDismissBehaviorというメソッドが使用できるようになりました。
これはメニュー内のボタンを押した後、メニューを閉じないようにすることができます。

実装

import SwiftUI

struct ContentView: View {
    @State private var searchText = ""
    
    var body: some View {
        Menu {
            Button {
                print("heart")
            } label: {
                Label("heart", systemImage: "heart.fill")
            }
            
            Button {
                print("star")
            } label: {
                Label("star", systemImage: "star.fill")
            }
            
            Button {
                print("cross")
            } label: {
                Label("cross", systemImage: "cross.fill")
            }
        } label: {
            Text("メニュー")
        }
+       .menuActionDismissBehavior(.disabled)
    }
}

比較

enabled disabled
Simulator Screen Recording - iPhone 15 - 2024-01-07 at 00.24.05.gif Simulator Screen Recording - iPhone 15 - 2024-01-07 at 00.24.32.gif

おわり

メニュー内のボタンを押した時にメニューを閉じたくない場合ってあんまり考えられないですね。
まぁ自由度が上がって嬉しいですね

公式ドキュメント

0
1
0

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
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?