ios - SwiftUI .contextMenu 使用 clipShape 在我的视图周围有可见的填充
问题描述
我在聊天气泡中有一张图片。这种效果是使用一个clipShape 来为气泡自定义形状来实现的。
当我附加一个 .contextMenu 并呈现 contextMenu 时,它会在聊天气泡周围显示一个填充。contextMenu 的默认cornerRadius 也剪裁了聊天气泡提示的一小部分。
我想要实现的是我可以在 Apple Messages 应用程序中看到的内容。在带有文本的消息上显示 contextMenu 会保留文本的 clipShape 而不添加填充。在图像上,它会删除 clipShape 并将图像调整为适当的纵横比。这两个我都无法实现。
struct ContentView: View {
var body: some View {
VStack {
Image("leaf")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 264, height: 361)
.clipShape(Bubble(location: .rightBottom))
.contentShape(Bubble(location: .rightBottom))
.contextMenu(ContextMenu(menuItems: {
Button {
} label: {
HStack {
Text("Save")
Image(systemName: "arrow.down.to.line")
}
}
}))
}
}
}
我曾尝试将 .contentShape 用于相同形状的气泡,但这根本没有效果。无论如何在下面的屏幕截图中显示没有圆角和左侧额外填充的 contextMenu?或者理想地使 contextMenu 容器背景清晰?
按下上下文菜单之前的图像:
解决方案
解决方案非常简单。只需使用内容形状的第一个参数类型:
func contentShape<S>(_ kind: ContentShapeKinds, _ shape: S)
并将 kind 设置为 .contextMenuPreview:
Image("leaf")
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 264, height: 361)
.clipShape(Bubble(location: .rightBottom))
.contentShape(.contextMenuPreview, Bubble(location: .rightBottom))
.contextMenu(ContextMenu(menuItems: {
Button {
} label: {
HStack {
Text("Save")
Image(systemName: "arrow.down.to.line")
}
}
}))
推荐阅读
- java - 在 application.properties 中使用环境变量时构建 Java 应用程序
- reactjs - 使用 npx 运行 create-react-app 的问题
- kotlin - 如何使用带有输入流的图像 URI - 使用 OkHttp 上传图像
- solidity - 错误:尚未将 SimpleSmartContract 部署到检测到的网络(网络/工件不匹配)
- salesforce - 如何在 auraErrorMask 中显示 CSS 错误
- r - 在 x 轴和 y 轴“时间序列”R 上绘制日期
- node.js - 如何检查电子邮件是否已经存在,不包括当前记录猫鼬
- linux - tflite_runtime 获取树莓派上的非法指令
- caching - Infinispan - 我可以为服务器中的每个 Web 应用程序创建 DefaultCacheManager
- python - 如何在 Python 中将 dict 作为具有相同索引的对象组合到 json 文件中?