Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermine scroll offset for UITextView in UIScrollVIew
    text
    copied!<p>I have <code>UITextView</code> and other views below and after textView in <code>UIScrollView</code>. When user enter text I change textView frame height on textView.contentSize.height. The idea is save the possibility for user see all text and other elements. I have some issue. When user tap I want to scroll to this line. And this must seems like: </p> <p><img src="https://i.stack.imgur.com/ldEEC.png" alt="enter image description here"></p> <p>I can't determine how calculate offset and always scroll to current caret like.</p> <p>My initial screen:</p> <p><img src="https://i.stack.imgur.com/XPpsH.png" alt="enter image description here"></p> <p>All views are subviews of <code>UIScrollView</code>. Every time when user enter <code>@"\n"</code> I change <code>UITextView</code> frame:</p> <pre><code>CGRect textViewFrame = textView.frame; CGSize textViewContentSize = textView.contentSize; NSLog(@"textView content size: %@", NSStringFromCGSize(textViewContentSize)); textViewFrame.size.height = textViewContentSize.height; [textView setFrame:textViewFrame]; </code></pre> <p>And I increment <code>UIScrollView</code> contentSize. My problem - I don't understand how to scroll to textView's <strong>CURSOR</strong> when it under keyboard. I try do something: </p> <pre><code>CGPoint cursorPosition = [textView caretRectForPosition:textView.selectedTextRange.start].origin; CGPoint relativeCursorPoint = [textView convertPoint:cursorPosition toView:scrollView]; if(textView.isFirstResponder &amp;&amp; relativeCursorPoint.y &gt;= scrollViewFrame.size.height + scrollView.contentOffset.y) { int offset = relativeCursorPoint.y - scrollViewFrame.size.height + 18.0f; //int offset = textViewRect.origin.y - scrollViewFrame.size.height + 18.0f; [scrollView setContentOffset:CGPointMake(0, offset) animated:YES]; } </code></pre> <p>But it didn't work.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload