Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Re-enable UITextView scrolling after it expands, with AutoLayout, to a specified height

I understand there are some similar posts but they don't seem to work for me.

I have a uitextview that I want to behave similar to messenging apps.

I used autolayouts and [textview setScrollEnabled:NO] to allow the uitextview to expand dynamically above the keyboard. Of course, it grows till it reaches the top of the screen. When it stops growing you cant see any additional text.

I tried to find the height where it stop growing inside the textViewDidChange and called [textView setScrollEnabled:YES], but it shrinked back the initial size before it grew.

How can i enable the scrolling when it reaches a certain height or cant grow anymore?

like image 397
marshy101 Avatar asked Jan 10 '23 08:01

marshy101


2 Answers

Best solution i've found till now was to leave scrolling enabled and changing textview's height according to it's content size.

Looks something like this:

- (void)textViewDidChange:(UITextView *)textView
{
    self.consTextViewHeight.constant = MIN(MAX_HEIGHT, textView.contentSize.height + textView.textContainerInset.top);
}
like image 150
chents Avatar answered Jan 21 '23 13:01

chents


Have you tried to setContentSize for textView? You can do that in viewDidLayoutSubviews if you want to wait for autolayout to finish.

Here is helpful link : how set content size of textView's Scrollview immediately when user starts scrolling or when tap on textview

NOTE: this should be a comment, but I don't have enough reputation for that...

like image 34
Miknash Avatar answered Jan 21 '23 11:01

Miknash