【问题标题】:SwiftUI TextEditor overlay ignores allowsHitTestingSwiftUI TextEditor 覆盖忽略 allowHitTesting
【发布时间】:2021-02-08 22:26:53
【问题描述】:

我使用 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


    【解决方案1】:

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

    【讨论】:

    • 谢谢,这是一个有效的解决方法。我最终在实际的 TextEditor 上设置了占位符,降低了不透明度和点击手势识别器,删除了该文本并将不透明度重置为 1。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 2020-03-30
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    相关资源
    最近更新 更多