呈现 Modal 全屏 SwiftUI

2023-12-28

我怎样才能呈现一个将占据全屏并且不能通过向下滑动来关闭的模式?目前我正在使用.sheet旨在提出一种可驳回的模式。

我没有注意到 Xcode 中的任何 beta 更改会改变此行为。

任何帮助,将不胜感激 :)


SwiftUI 1.0

我不确定这是否是您想要的,但可以通过使用 ZStack 和状态变量来控制它的隐藏/显示来创建您自己的模式屏幕。

Code

struct CustomModalPopups: View {
    @State private var showingModal = false
    
    var body: some View {
        ZStack {
            VStack(spacing: 20) {
                Text("Custom Popup").font(.largeTitle)
                
                Text("Introduction").font(.title).foregroundColor(.gray)
                
                Text("You can create your own modal popup with the use of a ZStack and a State variable.")
                    .frame(maxWidth: .infinity)
                    .padding().font(.title).layoutPriority(1)
                    .background(Color.orange).foregroundColor(Color.white)
                
                Button(action: {
                    self.showingModal = true
                }) {
                    Text("Show popup")
                }
                Spacer()
            }
            
            // The Custom Popup is on top of the screen
            if $showingModal.wrappedValue {
                // But it will not show unless this variable is true
                ZStack {
                    Color.black.opacity(0.4)
                        .edgesIgnoringSafeArea(.vertical)
                    // This VStack is the popup
                    VStack(spacing: 20) {
                        Text("Popup")
                            .bold().padding()
                            .frame(maxWidth: .infinity)
                            .background(Color.orange)
                            .foregroundColor(Color.white)
                        Spacer()
                        Button(action: {
                            self.showingModal = false
                        }) {
                            Text("Close")
                        }.padding()
                    }
                    .frame(width: 300, height: 200)
                    .background(Color.white)
                    .cornerRadius(20).shadow(radius: 20)
                }
            }
        }
    }
}

Example

(Excerpt from "SwiftUI Views" book) SwiftUI Views Book Excerpt So here, your popup is small, but you can adjust the dimensions to make it fullscreen with the frame modifier that is on that VStack.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

呈现 Modal 全屏 SwiftUI 的相关文章

随机推荐