@Binding 和向下滚动以关闭呈现的视图之间有什么不同?

What is different between @Binding and scroll down to dismiss presented view?

关闭视图后 NavigationBarItem 无法点击!

XCode11 beta3, MacOS Catalina 10.15 Beta(19A501i)

当通过@Binding点击DetailView按钮关闭时, ContentView 的 navigationBarItem 将被禁用(无法点击)! 但是向下滚动关闭就可以了(可以点击并在调试预览模式下打印"Clicked!")

struct DetailView: View {
    @Binding var isPresented: Bool
    var body: some View {
        Group {
            Text("Detail")
            Button(action: {
                self.isPresented.toggle()
            }) {
                Text("Dismiss")
            }
        }

    }
}

struct ContentView : View {
    @State var isPresented = false

    var body: some View {
        NavigationView{

            Button(action: {self.isPresented.toggle()}){
                Text("Show")
            }

            .presentation(!isPresented ? nil :
                Modal(DetailView(isPresented: $isPresented)) {
                    print("dismissed")
                }
            )

            .navigationBarTitle(Text("Test"))
            .navigationBarItems(trailing:
                Button(action: {print("Clicked!")} ) {
                    Image(systemName: "plus")
                        .frame(width: 44, height: 44)
                        .foregroundColor(.black)
                        .cornerRadius(22)
                }
                .padding(.trailing)
            )
        }
    }
}

我倾向于认为模态框存在错误。当模态消失时,onDismiss 永远不会被调用。但是,我确实找到了解决方法。我没有通过在模态视图内部设置 isPresented 变量来关闭,而是使用主 window 中的 rootViewController 来调用 UIKit 关闭方法。

通过这种方式关闭模态,正确调用了 onDismiss 闭包,我在其中设置了 isPresented = false,因此可以再次显示模态。

以下代码有效,至少在新版本修复问题之前是这样:

import SwiftUI

struct DetailView: View {

    var body: some View {
        Group {
            Text("Detail")
            Button(action: {
                UIApplication.shared.windows[0].rootViewController?.dismiss(animated: true, completion: { })
            }) {
                Text("Dismiss")
            }
        }

    }
}

struct ContentView : View {
    @State var isPresented = false

    var body: some View {
        NavigationView{

            Button(action: {self.isPresented.toggle()}){
                Text("Show")
            }

                .presentation(!isPresented ? nil :
                    Modal(DetailView()) {
                        self.isPresented = false
                        print("dismissed")
                    }
            )

                .navigationBarTitle(Text("Test"))
                .navigationBarItems(trailing:
                    Button(action: {print("Clicked!")} ) {
                        Image(systemName: "plus")
                            .frame(width: 44, height: 44)
                            .foregroundColor(.black)
                            .cornerRadius(22)
                    }
                    .padding(.trailing)
            )
        }
    }
}