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?

More than 1 year has passed since last update.

[SwiftUI] Menuが表示されているときにSheetが表示されない場合の応急措置

Last updated at Posted at 2022-09-13

はじめに

あくまで応急処置です。根本的な解決方法等ご存知の方コメントお待ちしております。

事象

Menuが表示されているときにSheetを表示させようとしても、表示されない
sheet bug

バグが発生するコード

iOS15.5で確認

struct MenuAlert: View {
    @State var isPresented = false
    
    var body: some View {
        VStack {
            Menu("menu") {
                Text("a")
            }
            
            Spacer()
            
            Button("show sheet: \(isPresented.description)") {
                isPresented = true
            }
        }
        .sheet(isPresented: $isPresented) {
            Text("sheet is presented")
        }
    }
}

対処方法

sheetを表示する前に以下のコードでmenuをdismissする

UIApplication.shared.windows.first?.rootViewController?.dismiss(animated: true)
struct MenuAlert: View {
    @State var isPresented = false
    
    var body: some View {
        VStack {
            Menu("menu") {
                Text("a")
            }
            
            Spacer()
            
            Button("show sheet: \(isPresented.description)") {
+                UIApplication.shared.windows.first?.rootViewController?.dismiss(animated: true)
                isPresented = true
            }
        }
        .sheet(isPresented: $isPresented) {
            Text("sheet is presented")
        }
    }
}

不思議な点

以下のように、Spacerを除去すると正常に動作する
なぜ🤔

struct MenuAlert: View {
    @State var isPresented = false
    
    var body: some View {
-        VStack {
+        VStack(spacing: 400) {
            Menu("menu") {
                Text("a")
            }
            
-           Spacer()

            Button("show sheet: \(isPresented.description)") {
                isPresented = true
            }
        }
        .sheet(isPresented: $isPresented) {
            Text("sheet is presented")
        }
    }
}
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?