簡體   English   中英

以編程方式創建的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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM