iOS 15请参见采纳的回答。这个解决方案适用于iOS 14。
我通过在图像上添加.contentShape(Rectangle())
修饰符来解决了这个问题:
Image("My Image")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 300, height: 250)
.clipped()
.contentShape(Rectangle())
.contextMenu {
Text("Menu Item")
}
func contentShape<S>(_ kind: ContentShapeKinds, _ shape: S)
将其设置为.contextMenuPreview,在所有形状上都可以工作:
Image("leaf")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 300, height: 300)
.clipShape(Circle())
.contentShape(ContentShapeKinds.contextMenuPreview, Circle())
.contextMenu {
Text("Menu Item")
}
contentShape
API。我刚刚接受了你的答案,谢谢分享! - Wilson Gramer