0

I have multiple text editors inside a scroll view and I want the text editors to not be scrollable. Currently, if the cursor is located inside a text editor, then the latter is being scrolled and not the scroll view. How can I prevent this from happening?

enter image description here

struct ContentView: View {
    var body: some View {
        ScrollView(.vertical) {
            ForEach(1 ... 30, id: \.self) { _ in
                ElementView(text: "")
            }
        }
        .padding()
    }
}

struct ElementView: View {
    @State var text: String

    var body: some View {
        ZStack(alignment: .leading) {
            TextEditor(text: $text)

            Text(text)
                .opacity(0)
                .fixedSize(horizontal: false, vertical: true)
        }.border(Color.red)
    }
}
mahan
  • 8,785
  • 1
  • 26
  • 60
bler
  • 37
  • 3
  • 1
    basically for this kind of customisation, you should build your own custom TextEditor – swiftPunk Jan 31 '21 at 14:27
  • Does this answer your question? [How do I create a multiline TextField in SwiftUI?](https://stackoverflow.com/questions/56471973/how-do-i-create-a-multiline-textfield-in-swiftui/58639072#58639072) – mahan Jan 31 '21 at 15:01
  • @swiftPunk Could you please give some more information as to what my custom TextEditor would need, for it to be non-scrollable? – bler Jan 31 '21 at 15:48
  • @mahan No, that's a different thing that I already have implemented. See the `ZStack` inside `ElementView`. My `TextEditor` is dynamic in height, but still scrollable. I'd like it to not be scrollable. – bler Jan 31 '21 at 15:49
  • You have not understood the problem. If you increase the height of the text-edtior, it won't scoll any more. – mahan Jan 31 '21 at 16:05
  • @bler: about your question non-scrollable, when you develop your custom TextEditor you can put a property which define your custom textEditor can be or can not be scrollable, like a switch, you decide when you want this functionality or not. – swiftPunk Jan 31 '21 at 18:04
  • @mahan Your updated link doesn't fit my case. Note that I'm building a MacOS app and UI components are a part of `UIKit`, which is for iOS. Increasing the height of the `TextEditor` doesn't help. Please look at the gif I've posted; the `TextEditor` is the same height as the text, but I'm still able to scroll in it. – bler Jan 31 '21 at 21:21
  • Have you tried using a UITextView instead? – mahan Feb 01 '21 at 09:29

1 Answers1

-2

unchecking all checkboxes in Behavior and Scrolling should do the trick!

image example