swiftui - SwiftUI TextEditor 覆盖忽略 allowHitTesting
问题描述
我使用 Text 作为TextEditor
. 无论我如何添加它,通过ZStack
或.overlay
,它都会取消TextEditor
. 所以当我点击文本/占位符时,文本编辑器不会激活,如果我点击它,它会激活。这可以在以下示例视图中重现:
struct ContentView: View {
@State private var text = ""
var body: some View {
ZStack {
TextEditor(text: $text)
.padding()
Text("blabla")
.allowsHitTesting(false)
}
}
}
解决方案
您可以在视图的初始化程序中添加它(或者在主 App 初始化程序中更好),然后,您可以使用 background 修饰符UITextView.appearance().backgroundColor = .clear
,而不是使用 ZStack 或覆盖在 a 的顶部添加一个占位符,如下所示:TextEditor
.background(Text("Placeholder..."))
推荐阅读
- dji-sdk - 我可以使用 DJI Drone 模拟器测试我的 DJI SDK for Windows 10 应用程序,而不会有运行真正 DJI Drone 的风险吗?
- firebase - 如何将文档添加到云 Firestore 中的集合
- javascript - doc.data() 返回空对象属性,但它在 console.log 上
- node.js - 在 Angular 中,如何解析和提取具有复杂结构 (JSON) 的 HTTP 响应?
- java - 如何使用带有 Spring Security 5.1+ 的 google OIDC 对用户进行身份验证
- javascript - 如何将我的输入字段和按钮向上对齐以使其与其他按钮内联?
- google-analytics - gatsby-plugin-google-analytics 允许回调或设置要求选项
- visual-studio-code - VS 代码扩展 - 传播捕获的键绑定
- python - 为什么别名在 Pandas 库中某些方法的前面
- sql - 如何在 SAP HANA Scalar User Defined Functions (UDF) 中编写 SQL 语句?