Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is what I ended up doing, and something that seems to work:</p> <pre><code>- (void)textViewKeyboardWillShow:(NSNotification *)notification { NSDictionary* info = [notification userInfo]; CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size; // self.textViewBottomSpace.constant = NSLayoutConstraint in IB (bottom position) self.textViewBottomSpace.constant = kbSize.height + 70; [self.textView setNeedsDisplay]; } - (void)textViewKeyboardWillHide:(NSNotification *)notification { self.textViewBottomSpace.constant = 0; [self.textView setNeedsDisplay]; } - (void)scrollCaretToVisible { //This is where the cursor is at. CGRect caretRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.end]; if(CGRectEqualToRect(caretRect, _oldRect)) return; _oldRect = caretRect; //This is the visible rect of the textview. CGRect visibleRect = self.textView.bounds; visibleRect.size.height -= (self.textView.contentInset.top + self.textView.contentInset.bottom); visibleRect.origin.y = self.textView.contentOffset.y; //We will scroll only if the caret falls outside of the visible rect. if(!CGRectContainsRect(visibleRect, caretRect)) { CGPoint newOffset = self.textView.contentOffset; newOffset.y = MAX((caretRect.origin.y + caretRect.size.height) - visibleRect.size.height + 10, 0); [self.textView setContentOffset:newOffset animated:YES]; } } - (void)textViewDidEndEditing:(UITextView *)textView { [_caretVisibilityTimer invalidate]; _caretVisibilityTimer = nil; } - (void)textViewDidBeginEditing:(UITextView *)textView { self.oldRect = [self.textView caretRectForPosition:self.textView.selectedTextRange.end]; self.caretVisibilityTimer = [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(scrollCaretToVisible) userInfo:nil repeats:YES]; } </code></pre>
 

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