问题:UITextView
默默地改变它contentSize
在某些情况下。
最简单的情况是带有大文本和键盘的textView。只需添加 UITextView 出口并设置- viewDidLoad
as:
- (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);
}
现在显示和隐藏键盘在某些情况下会导致文本跳转。
我找到了子类跳转的原因UITextView
。我的子类中唯一的方法是:
- (void)setContentSize:(CGSize)contentSize {
NSLog(@"CS: %@", NSStringFromCGSize(contentSize));
[super setContentSize:contentSize];
}
它显示contentSize
在键盘隐藏上缩小和扩展。像这样的事情:
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}
看起来像的行为UITextView
iOS7中发生了很大的变化。有些东西现在已经坏了。
进一步发现我发现了新的layoutManager
我的 textView 的属性也发生了变化。现在日志中有一些有趣的信息:
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
下一行内容为 contentSize ={320, 885}
包含Layout tree contents: ..., 2127 laid glyphs, 51 laid line fragments
。因此,看起来某种自动布局尝试在键盘显示上重新布局 textView 并更改 contentSize,即使布局尚未完成。即使我的 textView 在键盘显示/隐藏之间没有变化,它也会运行。
问题是:如何防止contentSize改变?