以下代码以编程方式创建一个文本视图,该文本视图在文本超出设备屏幕高度时滚动.代码在iOS 7和iOS 8中没有问题.但是,文本字段不再使用相同代码的iOS 9滚动.通过几个小时的尝试找到修
- (void)createTextView
{
//1. Create the text storage that backs the editor.
NSDictionary *attrs = @{NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleBody]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:notesAndReference attributes:attrs];
_textStorage = [[SyntaxHighlightTextStorage alloc] init];
[_textStorage appendAttributedString:attrString];
CGRect newTextViewRect = self.view.bounds;
//2. Create the layout manager.
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
//3. Create a text container.
CGSize containerSize = CGSizeMake(newTextViewRect.size.width, CGFLOAT_MAX);
NSTextContainer *container = [[NSTextContainer alloc] initWithSize:containerSize];
container.widthTracksTextView = YES;
[layoutManager addTextContainer:container];
[_textStorage addLayoutManager:layoutManager];
//4. Create a UITextView.
textView = [[UITextView alloc] initWithFrame:newTextViewRect textContainer:container];
textView.scrollEnabled = YES;
textView.editable = YES;
textView.delegate = self;
[self.view addSubview:textView];
}
在尝试了几个小时来解决这个问题后,我终于添加了一行代码来禁用滚动,然后重新启用它,这解决了问题!滚动现在可以像以前一样工作.正如我在原始问题中提到的,代码适用于iOS 7和iOS 8,但现在不适用于iOS 9,因此不确定此问题是否代表了错误或预期行为的变化.任何评论都会受到欢迎.谢谢.这是修复:textView.scrollEnabled = NO; textView.scrollEnabled = YES;
