ios - 弹出视图推送当前视图 SwiftUI
问题描述
我试图从这里重用一些设计,但我无法让它在屏幕上居中,它将视图推到顶部。如何在不推送视图的情况下显示这样的弹出窗口,只需将其放在视图顶部即可。应该使用警报吗?如何?
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: {
推荐阅读
- git - 使用 Jenkins 从共享文件夹上的存储库中提取 Git 时挂起
- deployment - 如何一步部署 SSIS 项目和环境变量
- python - 如何在使用 Aiohttp 库执行函数之前返回响应?
- c# - 绑定到 ObservableCollection 内对象的 ObservableCollection 属性
- javascript - 如何修复 googleappmaker 中的“未定义 DocumentApp”
- c# - 将 DataGridViewButtonCell 转换为类型 DataGridViewDisableButtonCell 时出错
- python - 嵌入在合并中的 for 循环
- mysql - MySql - 在某个单词之后选择一些字符并将其添加到另一列
- c++ - 如何找到数组左右两侧的最大元素?
- java - Spring DI 同时具有两个构造函数