我有一个通过营养 api 进行搜索并在列表中显示搜索结果的列表。直到升级到 ios 18,一切都完美运行。
现在,创建列表时,有时 onTapGesture 会识别并加载 fooddetail 页面,而列表中的其他项目根本不注册点击?
为什么会突然改变呢?我现在不知道如何让列表中的每个项目都可点击。
List(searchResults, id: \.fid) { food in
VStack {
HStack {
VStack(alignment: .leading) {
Text(food.name)
.font(.system(size: 20))
.padding(.bottom, 4)
Text("\(food.brand) - \(extractCalories(from: food.calories) ?? "nil")")
.font(.system(size: 16))
}
.padding()
.foregroundColor(Color.white)
Spacer()
VStack {
Image(systemName: "chevron.right")
.foregroundColor(Color.white)
.padding(.trailing, 8)
}
}
.padding([.leading, .trailing], 8.0)
.padding(.top, 0)
.background(
Color(red: 40 / 255, green: 40 / 255, blue: 40 / 255)
.clipShape(RoundedRectangle(cornerRadius: 10))
)
}
.onAppear {
print(searchResults)
}
.contentShape(Rectangle()) // Ensures the entire area is tappable
/*.simultaneousGesture(TapGesture().onEnded {
print("TAPPED FOOD ID: \(food.fid)")
tappedFoodID = food.fid
tappedFoodName = food.name
showFoodItemDetail = true
})*/
.onTapGesture(count: 1) {
print("TAPPED FOOD ID: \(food.fid)")
tappedFoodID = food.fid
tappedFoodName = food.name
showFoodItemDetail = true
}
.listRowSeparator(.hidden)
.listRowBackground(Color.clear)
.scrollContentBackground(.hidden)
.padding(.horizontal, -16) // Remove horizontal padding for the whole list
.padding(.vertical, 0) // Remove vertical padding for the whole list
}
我遇到了同样的问题。我将代码修改为以下内容并且运行良好。
.contentShape(Rectangle()) .highPriorityGesture( TapGesture().onEnded { print("TAPPED") } )