ysnsyhn
ysnsyhn

Reputation: 507

Avoiding multiple Scroll when textview is used inside Scrollview ios

I added a textview inside a ScrollView. The problem happens when I increase the size of the textView. The first scroll happens until the end of the view. The second scroll happens in the textview for scrolling it to the end of the text. For reading the whole text the user has to scroll it 2 times which is unpractical.

The structure is as below;

-View -Scroll view -textview

1) Is there a way to fill the whole text in the view to avoid the scrolling in the textview?

2) Can I make the Scrollviews size as large as the text is?

3) If yes, how to do it?

Thank you in advance.

Textview inside Scrollview

End of the Scrollviews scrolling part which didn't show the whole text

End of the Textviews Scrolling part

Structure

Structure

Upvotes: 1

Views: 4851

Answers (1)

Tobias
Tobias

Reputation: 4397

I believe you're asking how to have just a single scrollable area, basically all the views of the ViewController.

Set your textView's scrollEnabled option to false. It should cause the intrinsic height of the textView to become the height to display all the text, so you'll no longer have a scroll view inside a scrollview. Make sure you don't specify the height of the textView, otherwise it'll cut off the text.

Okay. I've created a demo project with what I think you're looking for.

Here's some explanation.

The scrollView's constraints to it's parent and sibling views define it's frame size.

The constraints from the scrollView's subviews to the scrollView define the contentSize.

With that in mind. Set the scrollView to be centered in it's superview and have the same width and height. (Now the scrollView's frame is defined).

To define the scrollview's contentSize we setup top, leading, trailing, bottom constraints using the scrollView's children. (Now the scrollView's contentSize is created)

If you don't have to have scrolling in the horizontal direction you need to make sure the children's width is less than of equals to the scrollView's superview.

I hope this information helps.

Upvotes: 3

Related Questions