O UITextView em um UITableViewCell de redimensionamento automático mostra e oculta o teclado no iPad, mas funciona no iPhone
Eu implementei um UITableViewCell personalizado que inclui um UITextView que é redimensionado automaticamente conforme o usuário digita, semelhante ao campo "Notas" no aplicativo de contatos. Ele está funcionando corretamente no meu iPhone, mas quando estou testando no iPad, estou tendo um comportamento muito estranho: quando você chega ao final de uma linha, o teclado se oculta por um milissegundo e se mostra novamente imediatamente. Eu o consideraria apenas um bug peculiar, mas na verdade causa alguma perda de dados, pois se você estiver digitando, perde um ou dois caracteres. Aqui está o meu código:
O código// 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] > 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 < 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;
}
Os problemasEntão, aqui está o que está acontecendo
Este código está funcionando 100% corretamente no meu iPhone e no simulador de iPhone. Enquanto digito o texto, o UITextView cresce sem problemas e o UITableViewCell junto com ele.No simulador do iPad, no entanto, ele fica maluco. Funciona bem enquanto você digita na primeira linha, mas quando você chega ao final de uma linha, o teclado desaparece e reaparece imediatamente, de modo que, se o usuário continuar digitando, o aplicativo perderá um caractere ou dois.Aqui estão algumas notas adicionais sobre os comportamentos estranhos que notei, que podem ajudar a explicá-lo:Além disso, descobri que remover as linhas[myTableView beginUpdates]; [myTableView endUpdates];
na funçãotextViewDidChange:(UITextView *)textView
faz com que o UITextView cresça corretamente e também não mostre e oculte o teclado, mas, infelizmente, o UITableViewCell não cresce para a altura adequada.ATUALIZAR: Segueestas instruçõesAgora sou capaz de parar o estranho movimento do texto; mas o teclado ainda está escondido e aparecendo, o que é muito estranho.Alguém tem alguma idéia de como mostrar o teclado continuamente, em vez de ocultar e mostrar quando você chega ao final da linha no iPad?
P.S .: Não estou interessado em usar o ThreeTwenty.