gpt4 book ai didi

ios - TextEditor 在 SwiftUI 中被键盘遮挡

转载 作者:行者123 更新时间:2023-12-05 02:28:46 26 4
gpt4 key购买 nike

我希望我的 TextEditors 避免使用屏幕键盘,以便我可以输入内容并查看它:) 我想我可以针对 iOS 15 进行设置。我相信我在 Internet 上尝试了很多处理键盘事件的解决方案并尝试调整一些填充/偏移等,但它们都不适合我。似乎 TextFields 根本没有这个问题(至少在 iOS 15 中),因为它们保持可见(容器 View 根据需要滚动),即使键盘出现在屏幕上也是如此。我不知道为什么不免费提供此基本功能...UIKit/UITextView 似乎无需开发人员额外注意即可工作。

那么我需要做什么才能进入下面示例中的第三个文本编辑器(在注释部分)并立即开始输入,而无需手动 ScrollView ,以便编辑器可见我?

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 本身就是 ScrollView ,因此 OS 可能会混淆移动/偏移的内容/位置...无论如何,在这种情况下(或者你将所有这些编辑器包装在 ForEach 中的原因)的一种可能方法是使用可聚焦状态和明确地强制一切。

使用 Xcode 13.4/iOS 15.5 测试

demo

这是主要部分:

        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

关于ios - TextEditor 在 SwiftUI 中被键盘遮挡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72550273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com