1

I have a SwiftUI TextEditor , unlike Textfields that moves up when keyboard appears from this answer in this link Move TextField up when the keyboard has appeared in SwiftUI
the TextEditor does not respond to this keyboard modifiers and the keyboard hides a big chunk of the TextEditor view . Is there a bug with SwiftUI TextEditor . How can i do that ?

  • Welcome to SO - Please take the [tour](https://stackoverflow.com/tour) and read [How to Ask](https://stackoverflow.com/help/how-to-ask) to improve, edit and format your questions. Without a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) it is impossible to help you troubleshoot. – lorem ipsum Apr 05 '21 at 18:36

1 Answers1

0

Are you looking for a dynamic textEditor like this? I set a maximum and minimum height so that it does not grow infinitely but you can easily get rid of that.

    import SwiftUI

   struct ReflectionSpace: View {
    
    
    @State private var textEditorHeight : CGFloat = 100
    @State private var reflection = ""

    
    var body: some View {
        
        ScrollView {
            
            VStack{
                
                ZStack(alignment: .leading) {
                    
                    Text(reflection)
                        .font(.custom("Times", size: 12
                        ))
                        .lineSpacing(1)
                       .frame(minHeight: 50)
                        .frame(maxHeight: 300)
                        .foregroundColor(.clear)
                        .padding()
                        .background(GeometryReader {
                            Color.clear.preference(key: ViewHeightKey.self,
                                value: $0.frame(in: .local).size.height)
                        })
                    
                    
                    TextEditor(text: $reflection)
                        .font(.custom("Times", size: 12))
                        .padding(6)
                        .lineSpacing(1)
                        .background(Color.red)
                }
                .padding(20)
                .onPreferenceChange(ViewHeightKey.self) { textEditorHeight = $0 }
            }
        }
    }
}

struct ViewHeightKey: PreferenceKey {
    
    static var defaultValue: CGFloat { 0 }
    
    static func
    
    reduce(value: inout Value, nextValue: () -> Value) {
        
        value = value + nextValue()
         
    }
    
}   

struct ReflectionSpace_Previews: PreviewProvider {
    static var previews: some View {
        ReflectionSpace()
    }
}