swiftui - 激活 contextMenu 时 UIViewRepresentable 视图消失
问题描述
我正在尝试将上下文菜单与 UIViewRepresentable 一起使用。当上下文菜单被激活时, UIViewRepresentable 消失。
这是代码:
UIViewRepresentable 视图:
struct TestView: UIViewRepresentable {
func makeUIView(context: Context) -> some UIView {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 150, height: 150))
view.backgroundColor = UIColor.red
return view
}
func updateUIView(_ uiView: UIViewType, context: Context) {
//
}
}
内容视图:
struct ContentView: View {
var body: some View {
TestView()
.frame(width: 200, height: 200)
.contextMenu {
Text("Context Menu")
}
}
}
如何让 UIViewRepresentable 不消失?
解决方案
我最近遇到了这个确切的问题,我的UIViewRepresentable
视图返回一个 custom UIImageView
。修复是clipped()
使用上下文菜单向视图添加修饰符。不确定这是否适用于您的案例,因为我的案例涉及图像。
这是在运行 iOS 15.1 的 iPhone 12 Pro 上测试的。
推荐阅读
- python - TypeError:“方法”对象不能解释为整数
- google-cloud-platform - GCP:如何找到在特定时间运行的实例?
- angular - 将属于 innerHTML 的元素替换为另一个元素
- powershell - 用于获取 FIPS 算法状态的 Powershell 脚本
- java - 列表 API 按流 API 分组
- flutter - 从休息调用中获取地图结果列表
- python-3.x - WebDriverException:消息:未知错误:调用函数结果缺少“值”——在运行 send_keys 并更新 Chromedriver 之后
- java - 我应该在 Spring Boot REST API 中发布什么对象?
- javascript - Javascript 19位数字将最后一位数字更改为零
- python - 如何将 Discord.py 和 Flask 合并到一个文件中?