Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically created UITextView no longer scrolls with iOS 9

The following code programmatically creates a text view that scrolls when the text exceeds the device screen height. The code has worked without a problem with iOS 7 and iOS 8. However, the text field no longer scrolls with iOS 9 with the same code. This problem has been quite frustrating through several hours of trying to find a fix, so I very much would appreciate any help. Thanks in advance. Here is the code:

- (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];

}

like image 435
user183804 Avatar asked Aug 24 '26 10:08

user183804


1 Answers

After trying for hours to fix this issue, I finally added one line of code to disable scrolling before re-enabling it, which fixed the problem!. Scrolling now works, as before. As I mentioned in the original question, the code worked with iOS 7 and iOS 8, but not now with iOS 9, so not sure if this problem represents a bug or a change in expected behavior. Any comments would be welcome. Thanks. Here is the fix:

 textView.scrollEnabled = NO;
 textView.scrollEnabled = YES;
like image 99
user183804 Avatar answered Aug 26 '26 00:08

user183804



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!