以编程方式创建的 UITextView 不再滚动 iOS 9

Programmatically created UITextView no longer scrolls with iOS 9

以下代码以编程方式创建文本视图,当文本超过设备屏幕高度时该视图会滚动。该代码在 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;