ios - 弹出 View 推送当前 View SwiftUI

标签 ios swiftui

我正在尝试重用 here 中的一些设计,但我无法将其置于屏幕中心,它将 View 推到顶部。如何在不推送 View 的情况下显示这样的弹出窗口,只需将其放在 View 顶部即可。应该使用警报吗?怎么办?

enter image description here

import SwiftUI

struct DatePicker: View {
    
    @State var hours: Int = 0
    @State var minutes: Int = 0
    @State private var newEatenMealTime = Date()
    @State private var showingNewMealTime = false
    
    var body: some View {
       
        VStack {
            Button(action: {
                self.showingNewMealTime = true
            }) {
                Text("Click me")
            }
            
            if $showingNewMealTime.wrappedValue {
                VStack(alignment: .center) {
                    ZStack{
                        Color.black.opacity(0.4)
                            .edgesIgnoringSafeArea(.vertical)
                        // this one is it
                        VStack(spacing: 20) {
                            Text("Time between meals")
                                .bold().padding()
                                .frame(maxWidth: .infinity)
                                .background(Color.yellow)
                                .foregroundColor(Color.white)

                            HStack {
                                Spacer()
                                VStack {
                                    Picker("", selection: $hours){
                                        ForEach(0..<4, id: \.self) { i in
                                            Text("\(i) hours").tag(i)
                                        }
                                    }
                                    .frame(width: 150, height: 120)
                                    .clipped()
                                }

                                VStack {
                                    Picker("", selection: $minutes){
                                        ForEach(0..<60, id: \.self) { i in
                                            Text("\(i) min").tag(i)
                                        }
                                    }
                                    .frame(width: 150, height: 120)
                                    .clipped()
                                }
                            }

                            Spacer()

                            Button(action: {
                                self.showingNewMealTime = false
                            }){
                                Text("Close")
                            } .padding()
                        }
                        .frame(width:300, height: 300)
                        .background(Color.white)
                        .mask(RoundedRectangle(cornerRadius: 20))
                        .shadow(radius: 20)
                    }
                }
            }
        }
        
        
        
        
        
        
    }
}

struct DatePicker_Previews: PreviewProvider {
    static var previews: some View {
        DatePicker()
    }
}

如果您在顶部看到,“点击我”不应该在那里,“点击我”位于屏幕中央,一旦弹出窗口出现,它就会被推送。

最佳答案

只需使用 ZStack 而不是 VStack。使用 Xcode 11.4/iOS 13.4 进行测试

var body: some View {

    ZStack {                // << here
        Button(action: {

关于ios - 弹出 View 推送当前 View SwiftUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62781739/

相关文章:

ios - 我的录音机 SwiftUI/Storyboard 应用程序在加载时不断崩溃

ios - JQuery Mobile 和 Cordova 应用程序示例

iphone - 配置 UItableView 不在 iPhone 上滚动

ios - 如何在 Swift 中将数组存储到 PFFile 中?

ios - 在 UIViewControllerRepresentable 中,如何将 ObservedObject 的值传递给 View Controller 并在每次值更改时更新它?

SwiftUI - @Published 数组不更新 subview

ios - 当您的应用程序打开并位于前台时显示一个 iOS 通知横幅?

ios - 如何更改 UIImage/UIImageView 的单个像素的颜色

ios - 如何检测 iOS 版 SwiftUI 中的 TextField 何时失去焦点?

arrays - 在 SwiftUI 中更新/编辑数组元素的最佳方式