I am trying to manage scrollview height according to textview content height with other view.
Code:
struct TextviewWithScrollview: View {
@State private var textStyle = UIFont.TextStyle.body
@State private var textForTextView = "fdgfhjdsgfdhsgfdsfg dsfg dsfgdsfh fh sf hdsjklf dhsjkfhsdjkfdhsjkfadhsfkds fdshfjkldsh fjkldsh fdshfdshfdsfhsfdsfh sf ewf g iuf herjkdsjkjvdhsvdshvdsv dshv ds vads vds hvsdvjkds vds hviuds vhv disu ghdisuv g"
var body: some View {
ZStack {
Color.red
.ignoresSafeArea()
ScrollView {
VStack(alignment: .leading) {
TextView(isEditable: .constant(false), text: .constant(textForTextView), textStyle:$textStyle, didStartEditing: .constant(false),placeHolderText:"")
}
HStack {
Button("Top") {
}
Button("Middle") {
}
Button("Bottom") {
}
}
}
}
}
}
struct TextView: UIViewRepresentable {
@Binding var isEditable:Bool
@Binding var text: String
@Binding var textStyle: UIFont.TextStyle
@Binding var didStartEditing: Bool
var placeHolderText: String = ""
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.delegate = context.coordinator
textView.font = UIFont.preferredFont(forTextStyle: textStyle)
textView.autocapitalizationType = .sentences
textView.isSelectable = true
textView.isUserInteractionEnabled = true
textView.isScrollEnabled = true
textView.dataDetectorTypes = .all
textView.textColor = .white
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
uiView.font = UIFont.preferredFont(forTextStyle: textStyle)
}
func makeCoordinator() -> Coordinator {
Coordinator($text)
}
class Coordinator: NSObject, UITextViewDelegate {
var text: Binding<String>
init(_ text: Binding<String>) {
self.text = text
}
func textViewDidChange(_ textView: UITextView) {
DispatchQueue.main.async {
self.text.wrappedValue = textView.text
}
}
}
}
Output without Scrollview:

Output With Scrollview:

Can someone please explain to me how to manage Scrollview Height according to textview content with other's views. I've tried to implement by above but no results yet.
Any help would be greatly appreciated.
Thanks in advance.

