Mudanças do UITextView contentSize e NSLayoutManager no iOS7

O problema:UITextView muda silenciosamente écontentSize em algumas situações.

O mais simples caso textView com texto grande e teclado. Basta adicionar a saída UITextView e definir- viewDidLoad Como:

- (void)viewDidLoad {
    [super viewDidLoad];
    // expand default "Lorem..."
    _textView.text = [NSString stringWithFormat:@"1%@\n\n2%@\n\n3%@\n\n4%@\n\n5", _textView.text, _textView.text, _textView.text, _textView.text];
    _textView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;
    _textView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0);
}

Agora, mostrar e ocultar o teclado causará saltos de texto em alguns casos.

Eu encontrei o motivo de saltar por subclasseUITextView. O único método na minha subclasse é:

- (void)setContentSize:(CGSize)contentSize {
    NSLog(@"CS: %@", NSStringFromCGSize(contentSize));
    [super setContentSize:contentSize];
}

E isso mostracontentSize encolhe e expande no teclado hide. Algo assim:

013-09-16 14:40:27.305 textView-bug2[11087:a0b] CS: {320, 651}
2013-09-16 14:40:27.313 textView-bug2[11087:a0b] CS: {320, 885}
2013-09-16 14:40:27.318 textView-bug2[11087:a0b] CS: {320, 902}

Parece comportamento deUITextView foi mudado muito no iOS7. E algumas coisas estão quebradas agora.

Descobrindo mais, descobri que novaslayoutManager propriedade do meu textView também muda. Há algumas informações interessantes no log agora:

2013-09-16 14:41:59.352 textView-bug2[11115:a0b] CS: {320, 668}
<NSLayoutManager: 0x899e800>
    1 containers, text backing has 2129 characters
    Currently holding 2129 glyphs.
    Glyph tree contents:  2129 characters, 2129 glyphs, 3 nodes, 96 node bytes, 5440 storage bytes, 5536 total bytes, 2.60 bytes per character, 2.60 bytes per glyph
    Layout tree contents:  2129 characters, 2129 glyphs, 532 laid glyphs, 13 laid line fragments, 4 nodes, 128 node bytes, 1048 storage bytes, 1176 total bytes, 0.55 bytes per character, 0.55 bytes per glyph, 40.92 laid glyphs per laid line fragment, 90.46 bytes per laid line fragment

E a próxima linha com contentSize ={320, 885} contémLayout tree contents: ..., 2127 laid glyphs, 51 laid line fragments. Por isso, parece que algum tipo de autolayout tenta redistribuir o textView no teclado show e altera o contentSize mesmo que o layout ainda não esteja concluído. E ele roda mesmo que meu textView não seja alterado entre o teclado show / hide.

A questão é: como evitar alterações no contentSize?

questionAnswers(3)

yourAnswerToTheQuestion