简体   繁体   English

TextEditor 在 SwiftUI 中被键盘遮挡

[英]TextEditor is obscured by keyboard in SwiftUI

I would like my TextEditors to avoid on-screen keyboard so that I can type something in and see it :) I guess I'm fine with targeting iOS 15. I believe I tried many solutions on the Internet that handle keyboard events and try to adjust some paddings/offsets etc, but none of them worked for me.我希望我的 TextEditor 避免使用屏幕键盘,以便我可以输入一些内容并查看它:) 我想我可以针对 iOS 15。我相信我在 Internet 上尝试了许多处理键盘事件的解决方案并尝试调整一些填充/偏移等,但它们都不适合我。 Seems like Text Fields do not have this issue at all (at least in iOS 15) as they stay visible (container view is scrolled as needed) even when keyboard appears on screen.似乎文本字段根本没有这个问题(至少在 iOS 15 中),因为即使键盘出现在屏幕上,它们也保持可见(容器视图根据需要滚动)。 I have no idea why this essential feature is not given for free... UIKit/UITextView seems to work without additional care from developer side.我不知道为什么不免费提供这个基本功能...... UIKit/UITextView 似乎在没有开发人员额外照顾的情况下工作。

So what do I need to do in order to be able to tap into the 3rd text editor (in the Notes section) in the example below and start typing immediately without having to manually scroll the view so that the editor is visible for me?那么我需要做什么才能在下面的示例中进入第三个文本编辑器(在注释部分)并立即开始输入,而无需手动滚动视图以便编辑器对我可见?

import SwiftUI

struct ContentView: View {
    @State private var text: String = ""
    
    init() {
        UITextView.appearance().backgroundColor = .clear
    }
    
    var body: some View {
        Form {
            TextEditor(text: $text)
                .frame(height: 300)
                .background(.yellow)
            TextEditor(text: $text)
                .frame(height: 300)
                .background(.mint)
            Section("Notes") {
                TextEditor(text: $text)
                    .frame(height: 300)
                    .background(.teal)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

TextEditor is itself scroll view so OS just can be confused of what/where move/offset... Anyway, a possible approach in such situation (or whey you will wrap all those editors in ForEach) is to use focusable state and force everything up explicitly. TextEditor 本身就是滚动视图,因此操作系统可能会对移动/偏移的内容/位置感到困惑......无论如何,在这种情况下(或者乳清将所有这些编辑器包装在 ForEach 中)的一种可能方法是使用可聚焦状态并强制一切明确地。

Tested with Xcode 13.4 / iOS 15.5使用 Xcode 13.4 / iOS 15.5 测试

演示

Here is main part:这是主要部分:

        TextEditor(text: $text).id(2)
            .focused($inFocus, equals: 2)
            .frame(height: 300)
            .background(.teal)
        
        if inFocus == 2 {
            Color.clear.frame(height: 300)
        }
    }
    .onChange(of: inFocus) { id in
        withAnimation {
            sp.scrollTo(id)
        }
    }

Complete test code is here 完整的测试代码在这里

To make use of ScrollView's automatic KeyboardAvoidance, whilst making sure Form is satisfied being inside a ScrollView (which it isn't by default), you can set a fixed width for the Form using a frame.要使用 ScrollView 的自动 KeyboardAvoidance,同时确保 Form 位于 ScrollView 内(默认情况下不是这样),您可以使用框架为 Form 设置固定宽度。 The example below sets a frame that matches the size of the screen, but you can play around with the sizing if needed.下面的示例设置了一个与屏幕大小相匹配的框架,但如果需要,您可以随意调整大小。

let screenSize = UIScreen.main.bounds.size
    
    var body: some View {
        ScrollView {
            Form {
                TextEditor(text: $text)
                    .frame(height: 300)
                    .background(.yellow)
                TextEditor(text: $text)
                    .frame(height: 300)
                    .background(.mint)
                Section("Notes") {
                    TextEditor(text: $text)
                        .frame(height: 300)
                        .background(.teal)
                }
            }
            .frame(width: screenSize.width, height: screenSize.height)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM