Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...