首页 > 解决方案 > 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)
        }
    }
}

标签: swiftui

解决方案


您可以在视图的初始化程序中添加它(或者在主 App 初始化程序中更好),然后,您可以使用 background 修饰符UITextView.appearance().backgroundColor = .clear,而不是使用 ZStack 或覆盖在 a 的顶部添加一个占位符,如下所示:TextEditor.background(Text("Placeholder..."))


推荐阅读