我有一个主视图,名为 TaskManagementView,其中有列表,如果我单击,taskCell > RoundedRectangle
则如何data.id
在 TaskManagementView 上显示该单元格的 PopupView
像这样我需要:
代码:使用此代码,如果我点击,taskCell > RoundedRectangle
则 PopupView 会显示在所有单元格的侧面。如何修复并实现像上面屏幕那样的 o/p
struct TaskManagementView: View {
@Environment(\.dismiss) var dismiss
@StateObject private var viewModel = TaskManagementViewModel()
@State private var isComposeViewShown = false
var body: some View {
ZStack {
VStack(spacing: 0) {
VStack {
List {
ForEach(0..<viewModel.arrData.count, id: \.self) { i in
let data = viewModel.arrData[i]
Section {
if data.isExpanded {
ForEach(0..<data.items.count, id: \.self) { j in
taskCell(data: data.items[j])
.onTapGesture {
gotoTaskDetails = true
}
}
}
}
}
}
}
}
.onAppear {
viewModel.fetchTasks(filterId: selectedFilterId) { status in
}
}
}
}
@ViewBuilder func taskCell(data: TaskManagementData) -> some View {
HStack(spacing: 16) {
RoundedRectangle(cornerRadius: 2)
.frame(width: 12, height: 12)
.foregroundStyle(Color.orange)
.onTapGesture {
print("testing data id... \(data.id)")
isComposeViewShown.toggle()
}
if isComposeViewShown {
PopupView(isComposeViewShown: $isComposeViewShown)
}
VStack(alignment: .leading, spacing: 2) {
Text(data.taskTitle ?? "")
.lineLimit(1)
}
}
}
}
struct PopupView: View {
@Binding var isComposeViewShown: Bool
var body: some View {
ZStack {
Color.black.opacity(0.5)
.edgesIgnoringSafeArea(.all)
.onTapGesture {
isComposeViewShown = false
}
//some other code of popup design
.padding(.bottom, 150)
}
}
}
上述代码的 o/p 不好:为什么 PopupView 显示在单元格内?我需要 TaskManagementView 上的 PopupView
你把弹出窗口放在里面
taskCell
意味着它将显示在taskCell
屏幕上的每一个您将弹出窗口放置在一个
HStack
含义上,它将与其余taskCell
内容一起水平显示。要解决此问题,请使用
ZStack
请参阅如何在 SwiftUI 中使用堆栈视图,