[英]Scroll to bottom of UITextView erratic in iOS 7
以下代碼在iOS <7.0中可以正常工作。 在iOS 7中,當UITextView正在更新時,滾動將變得不穩定且不穩定。 我不確定這是否是iOS 7中的錯誤,或者我做錯了什么。
TestController.h
//TODO: Add UITextView in storyboard and tie to textView outlet
#define MAX_TEXT_VIEW_CHARACTERS 1000
@interface TestController : UIViewController {
NSMutableString *_outputText;
NSTimer *_outputTimer;
}
@property (strong, nonatomic) IBOutlet UITextView *textView;
@end
TestController.m
@implementation TestController
@synthesize textView;
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
_outputText = [NSMutableString stringWithCapacity:MAX_TEXT_VIEW_CHARACTERS];
_outputTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(outputLine:) userInfo:nil repeats:YES];
}
-(void)outputLine:(NSTimer *) theTimer {
static int i = 0;
//Run this 100 times
if (i > 99) {
[_outputTimer invalidate];
return;
}
[self outputToScreen:[NSString stringWithFormat:@"Some string %d\r", ++i]];
}
-(void)outputToScreen:(NSString *)str {
if (!str || !str.length) return; //Nothing to output
NSInteger outputTextSize = _outputText.length;
[_outputText appendString:str];
if (outputTextSize > MAX_TEXT_VIEW_CHARACTERS)
[_outputText deleteCharactersInRange:NSMakeRange(0, outputTextSize - MAX_TEXT_VIEW_CHARACTERS)];
self.textView.text = _outputText;
[self scrollOutputToBottom];
}
-(void)scrollOutputToBottom {
CGPoint p = [textView contentOffset];
[textView setContentOffset:p animated:NO];
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
@end
這適用於iOS7。
-(void) scrollToBottom {
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
[textView setScrollEnabled:NO];
[textView setScrollEnabled:YES];
}
這顯然是iOS 7的錯誤。 這是一個解決方法,直到蘋果修復它。 解決方法基本上是通過從頭創建NSTextStorage
和NSLayoutManager
來實例化UITextView
。 Apple必須忘記在UITextView
初始化方法中初始化一些東西。 我提交了一份錯誤報告,我希望你也這樣做。
// ios7 bug fix
// check if the device is running iOS 7.0 or later
NSString *reqSysVer = @"7.0";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending);
if (osVersionSupported) {
NSTextStorage* textStorage = [[NSTextStorage alloc] init];
NSLayoutManager* layoutManager = [NSLayoutManager new];
[textStorage addLayoutManager:layoutManager];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:self.view.bounds.size];
[layoutManager addTextContainer:textContainer];
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView
textContainer:textContainer];
// if using ARC, remove these 3 lines
[textContainer release];
[layoutManager release];
[textStorage release];
}
else {
yourTextView = [[UITextView alloc] initWithFrame:someFrameForYourTextView];
}
iOS 7中有兩個問題可以解釋您的問題:
解決方案可能是:
-(void)scrollOutputToBottom {
CGRect caretRect = [textView caretRectForPosition:textView.endOfDocument];
[textView scrollRectToVisible:caretRect animated:NO];
}
嘗試這個:
// Don't forget to set textView's delegate
-(void)textViewDidChangeSelection:(UITextView *)textView {
[textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
}
對於那些使用Swift的人,我在這里發布與RawMean相同的答案(再次感謝!)。 正如我寫的那樣(2014年12月),問題仍然存在於iOS 8.1中,他的解決方案完美地工作......
var textView: UITextView!
var textStorage: NSTextStorage!
var layoutManager: NSLayoutManager!
var textContainer: NSTextContainer!
override func viewDidLoad() {
textStorage = NSTextStorage()
layoutManager = NSLayoutManager()
textStorage.addLayoutManager(layoutManager)
let newTextViewRect = view.bounds
let containerSize = CGSize(width: newTextViewRect.width, height: CGFloat.max)
textContainer = NSTextContainer(size: containerSize)
layoutManager.addTextContainer(textContainer)
textView = UITextView(frame: newTextViewRect, textContainer: textContainer)
textView.delegate = self
view.addSubview(textView)
}
override func viewDidLayoutSubviews() {
textView.frame = view.bounds
}
我使用scrollRangeToVisible方法在文本添加時在底部平滑滾動...
let length = countElements(textView.text)
let range:NSRange = NSMakeRange(length - 1, 1)
textView.scrollRangeToVisible(range)
基本上setScrollEnabled = YES需要在調用layoutSubviews之前設置。 它對我有用。
這對我行得通。 參考: UITextView setText不應該跳轉到ios8的頂部
self.textView.layoutManager.allowsNonContiguousLayout = NO;
self.textView.text = fileContent;
if(fileContent.length > 1)
{
NSRange range = NSMakeRange(self.textView.text.length - 1, 1);
[self.textView scrollRangeToVisible:range];
}
Swift 2.0 - IOS 8
這基本上是上面dklt的答案的Swift 2.0版本。 以前我使用相同的方法沒有2行scrollEnabled
。 大多數時候它工作正常。 但是,當幾乎同時快速連續調用scrollToBottom()
時,它有時不起作用。
scrollEnabled
的兩行沒有多大意義,但在添加它們之后,該方法始終如一 !
注:我試圖把2線scrollEnabled
在不同位置的前面或后面scrollRangeTovisible
,如dklt的答復的意見建議......
只有dklt的原始解決方案適合我。 其余的沒有。
func scrollToBottom()
{
let range:NSRange = NSMakeRange(self.textView.text.characters.count - 1, 1)
self.textView.scrollRangeToVisible(range)
self.textView.scrollEnabled = false
self.textView.scrollEnabled = true
}
請試試這個解決方案
-(void) scrollToBottom {
[textView setContentOffset:CGPointMake(0.0, textView.contentSize.height) animated:YES];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.