Note that there are some explanatory texts on larger screens.

plurals
  1. POUITextView in a UITableViewCell smooth auto-resize shows and hides keyboard on iPad, but works on iPhone
    text
    copied!<p>I have implemented a custom UITableViewCell which includes a UITextView that auto-resizes as the user types, similar to the "Notes" field in the Contacts app. It is working properly on my iPhone, but when I am testing it in the iPad, I am getting some very strange behavior: When you get to the end of a line, the keyboard hides for a millisecond and then shows itself again immediately. I would write it off as just a quirky bug, but it actually causes some data loss since if you are typing, it loses a character or two. Here's my code:</p> <h2>The Code</h2> <pre><code>// returns the proper height/size for the UITextView based on the string it contains. // If no string, it assumes a space so that it will always have one line. - (CGSize)textViewSize:(UITextView*)textView { float fudgeFactor = 16.0; CGSize tallerSize = CGSizeMake(textView.frame.size.width-fudgeFactor, kMaxFieldHeight); NSString *testString = @" "; if ([textView.text length] &gt; 0) { testString = textView.text; } CGSize stringSize = [testString sizeWithFont:textView.font constrainedToSize:tallerSize lineBreakMode:UILineBreakModeWordWrap]; return stringSize; } // based on the proper text view size, sets the UITextView's frame - (void) setTextViewSize:(UITextView*)textView { CGSize stringSize = [self textViewSize:textView]; if (stringSize.height != textView.frame.size.height) { [textView setFrame:CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, stringSize.height+10)]; // +10 to allow for the space above the text itself } } // as per: https://stackoverflow.com/questions/3749746/uitextview-in-a-uitableviewcell-smooth-auto-resize - (void)textViewDidChange:(UITextView *)textView { [self setTextViewSize:textView]; // set proper text view size UIView *contentView = textView.superview; // (1) the padding above and below the UITextView should each be 6px, so UITextView's // height + 12 should equal the height of the UITableViewCell // (2) if they are not equal, then update the height of the UITableViewCell if ((textView.frame.size.height + 12.0f) != contentView.frame.size.height) { [myTableView beginUpdates]; [myTableView endUpdates]; [contentView setFrame:CGRectMake(0, 0, contentView.frame.size.width, (textView.frame.size.height+12.0f))]; } } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { int height; UITextView *textView = myTextView; [self setTextViewSize:textView]; height = textView.frame.size.height + 12; if (height &lt; 44) { // minimum height of 44 height = 44; [textView setFrame:CGRectMake(textView.frame.origin.x, textView.frame.origin.y, textView.frame.size.width, 44-12)]; } return (CGFloat)height; } </code></pre> <h2>The Problems</h2> <p>So, here's what's happening</p> <ol> <li>This code is working 100% properly on my iPhone and in the iPhone simulator. As I type the text, the UITextView grows smoothly, and the UITableViewCell along with it.</li> <li>On the iPad simulator, however, it gets screwy. It works fine while you are typing on the first line, but when you get to the end of a line, the keyboard disappears and then reappears immediately, so that if the user continues typing the app misses a character or two.</li> <li>Here are some additional notes on the weird behaviors that I have noticed which may help explain it: <ul> <li>Also, I have found that removing the lines <code>[myTableView beginUpdates]; [myTableView endUpdates];</code> in the function <code>textViewDidChange:(UITextView *)textView</code> makes the UITextView grow properly and also doesn't show and hide the keyboard, but unfortunately, then the UITableViewCell doesn't grow to the proper height.</li> <li><strong>UPDATE:</strong> Following <a href="https://stackoverflow.com/questions/1178010/how-to-stop-uitextview-from-scrolling-up-when-entering-it/1864205#1864205">these instructions</a>, I am now able to stop the strange movement of the text; but the keyboard is still hiding and showing, which is very strange.</li> </ul></li> </ol> <p><strong>Does anyone have any ideas as to how to get the keyboard to continually show, rather than hide and show when you get to the end of the line on the iPad?</strong></p> <p>P.S.: I am not interested in using ThreeTwenty.</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