Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
580 views
in Technique[技术] by (71.8m points)

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

NavigationBarItem can't Click after dismiss view!

XCode11 beta3, MacOS Catalina 10.15 Beta(19A501i)

When click DetailView button to dismiss by @Binding, ContentView's navigationBarItem will disabled(Can't Click)! But scroll down to dismiss will be fine(can click and will be print "Clicked!" in Debug Preview Mode)

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)
            )
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm inclined to think that there is a bug with modals. The onDismiss is never called when the modal goes away. However, I did found a workaround. Instead of dismissing by setting the isPresented variable from inside the modal view, I use the rootViewController from the main window, to call the UIKit dismiss method.

By dismissing the modal this way, the onDismiss closure is called properly, and it is there where I set isPresented = false, so the modal can be presented again.

The following code works, at least until a new version fixes the problem:

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)
            )
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...