2

I am working on a feature for my app which will allow user to dictate text which will appear in a textField. For that I created a scrollView with only a textField which contents are automatically updating as user is dictating. However I came across a problem regarding the autoscrolling of the scrollView. Because the textFields height is growing with new text coming in at some point it is as height as the scroll view which means that at this point textFields height will continue to grow but the user will not see any new text.

How do I programmatically make sure that if the textfield outgrows in height the scrollView, the scrollview will scroll down to show new text?

I already tried to use the ScrollViewReader, ScrollViewProxy and the .onChange closure but it doesn't seem to have any affect.

ScrollViewReader{proxy in
                        ScrollView(.vertical, showsIndicators: false) {
                            Text(newMessageText)
                            .frame(width: 230, alignment: .center)
                            .font(.system(size: 46))
                            .foregroundColor(.black)
                            .multilineTextAlignment(.leading)
                            .lineLimit(nil)
                            .padding()
                            .id(textViewId)
                        }
                        .onChange(of: newMessageText) { newValue in
                            proxy.scrollTo(textViewId, anchor: .bottom)
                        }
                        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Mikolaj
  • 56
  • 5

0 Answers0