@Binding 和向下滚动以关闭呈现的视图有什么区别? [英] What is different between @Binding and scroll down to dismiss presented view?

查看:19
本文介绍了@Binding 和向下滚动以关闭呈现的视图有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NavigationBarItem 在关闭视图后无法点击!

NavigationBarItem can't Click after dismiss view!

XCode11 beta3,MacOS Catalina 10.15 Beta(19A501i)

XCode11 beta3, MacOS Catalina 10.15 Beta(19A501i)

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

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

推荐答案

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

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.

通过这种方式关闭模态,onDismiss 闭包被正确调用,并且在那里我设置 isPresented = false,因此模态可以再次呈现.

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

这篇关于@Binding 和向下滚动以关闭呈现的视图有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆