我正在尝试将上下文菜单选项添加到 iOS 应用程序中的 UICollection 视图。这是我的代码。此代码适用于最新的 iPhone,如 iPhone 12、13、14。但当我在 iPhone 7 上测试它时,无论是物理设备还是运行 iOS 15 的模拟器都不起作用。 iPhone 7 中不显示上下文菜单。由于 iPhone 7 支持 3D 触摸,我是否需要专门为 iPhone 7 实现 Peek 和 Pop?
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil,previewProvider: nil){ _ in
let delete = UIAction(title:"Delete",image: nil){ _ in
print("Delete")
}
return UIMenu(title: "",options: .displayInline,children: [delete])
}
return configuration
}
也许您可以使用 contextMenuConfigurationForItemAt 而不是 contextMenuConfigurationForItemsAt 来做到这一点?
func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? {
let configuration = UIContextMenuConfiguration(identifier: nil,previewProvider: nil){ _ in
let delete = UIAction(title:"Delete",image: nil){ _ in
print("Delete")
}
return UIMenu(title: "",options: .displayInline,children: [delete])
}
return configuration
}