0

我有一个 SwiftUI ListTextEditor最后一行是 a。在 SwiftUI 中,文本字段会自动向上移动到键盘上方。但不幸的是,当 TextEditor 在 a 中时,List它似乎不起作用。

TextField使用Ex:-当键盘出现在 SwiftUI 中时向上移动 TextField 时,有很多很好的解决方案。但是在使用时它们似乎都不起作用TextEditor


struct ContentView: View {

    @State var items: [String] = ["Apples", "Oranges", "Bananas", "Pears", "Mangos", "Grapefruit","Apples", "Oranges", "Bananas", "Pears", "Mangos", "Grapefruit"]

    @State private var newItemText : String = ""


    var body: some View {
        ZStack(alignment: Alignment.bottom) {
            List{
                 ForEach(items, id: \.self) { 
                     Text("\($0)")
                 }
                 TextEditor(text: $newItemText)
            }
            
        }
    }
}

4

1 回答 1

0

似乎视图确实会自动调整大小,使其位于键盘上方。所有需要做的就是滚动到列表的最后一行。

struct ContentView: View {

    @State var items: [String] = ["Apples", "Oranges", "Bananas", "Pears", "Mangos", "Grapefruit","Apples", "Oranges", "Bananas", "Pears", "Mangos", "Grapefruit"]

    @State private var newItemText : String = ""


    var body: some View {
      ScrollViewReader { (proxy: ScrollViewProxy) in
        ZStack(alignment: Alignment.bottom) {
            List{
                 ForEach(items, id: \.self) { 
                     Text("\($0)")
                 }
                 TextEditor(text: $newItemText)
                    .onChange(of: newItemText) { newValue in
                          proxy.scrollTo(999, anchor: .bottom)
                   }.id(999)
            }
            
        }
      }
    }
}
于 2021-11-19T16:38:13.563 回答