是否可以在 SwiftUI 中使模态不可关闭? [英] Is it possible to make a modal non-dismissible in SwiftUI?

查看:21
本文介绍了是否可以在 SwiftUI 中使模态不可关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,其中登录/注册部分位于模态内,如果用户未登录,则会显示该应用程序.

I am creating an App where the login / register part is inside a modal, which is shown if the user is not logged in.

问题是,用户可以通过向下滑动来关闭模态...

The problem is, that the user can dismiss the modal by swiping it down...

是否可以防止这种情况发生?

Is it possible to prevent this?

var body: some View {
    TabView(selection: $selection) {
        App()
    }.sheet(isPresented: self.$showSheet) { // This needs to be non-dismissible
        LoginRegister()
    }
}

第二个例子:

我正在使用模态来询问信息.用户不应该能够退出这个过程,除非通过保存按钮关闭模式.用户必须在按钮工作之前输入信息.不幸的是,可以通过向下滑动来关闭模态.

I am using a modal to ask for information. The user should not be able to quit this process except by dismissing the modal with save button. The user has to input information before the button works. Unfortunately the modal can be dismissed by swiping it down.

是否可以防止这种情况发生?

Is it possible to prevent this?

推荐答案

iOS 15 及更高版本:

在工作表上使用 .interactiveDismissDisabled(true),仅此而已.

Use .interactiveDismissDisabled(true) on the sheet, that's all.

旧版 iOS 15:

您可以尝试使用 highPriorityGesture 来做到这一点.当然,蓝色矩形仅用于演示,但您必须使用覆盖整个屏幕的视图.

You can try to do this by using a highPriorityGesture. Of course the blue Rectangle is only for demonstration but you would have to use a view which is covering the whole screen.

struct ModalViewNoClose : View {
    @Environment(\.presentationMode) var presentationMode
    
    let gesture = DragGesture()
    
    var body: some View {
        
        Rectangle()
            .fill(Color.blue)
            .frame(width: 300, height: 600)
            .highPriorityGesture(gesture)
            
            .overlay(
                VStack{
                    Button("Close") {
                        self.presentationMode.value.dismiss()
                    }.accentColor(.white)
                    Text("Modal")
                        .highPriorityGesture(gesture)
                    TextField("as", text: .constant("sdf"))
                        .highPriorityGesture(gesture)
                } .highPriorityGesture(gesture)
        )
            .border(Color.green)
    }
}

这篇关于是否可以在 SwiftUI 中使模态不可关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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