Scroll to bottom of UITextView erratic in iOS 7

2019-01-10 19:14发布

The following code will work fine in iOS < 7.0. In iOS 7 the scrolling will be choppy and erratic while the UITextView is updating. I'm not sure if this is a bug in iOS 7, or I am doing something wrong.

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

9条回答
成全新的幸福
2楼-- · 2019-01-10 19:48

Basically setScrollEnabled = YES need to be set before layoutSubviews get called. It worked for me.

查看更多
趁早两清
3楼-- · 2019-01-10 19:54

This works for me in iOS7.

-(void) scrollToBottom {
  [textView scrollRangeToVisible:NSMakeRange([textView.text length], 0)];
  [textView setScrollEnabled:NO];
  [textView setScrollEnabled:YES];
}
查看更多
仙女界的扛把子
4楼-- · 2019-01-10 19:54

Swift 2.0 - IOS 8

This is basically a Swift 2.0 version of dklt's answer above. Previously I was using the same method without the 2 lines of scrollEnabled. Most of the time it works fine. However, when scrollToBottom() is called in quick succession at almost the same time, it doesn't works sometimes.

The 2 lines of scrollEnabled doesn't makes much sense, but after adding them the method works consistently!

Note: I have tried to put the 2 lines of scrollEnabled in various position before or after the scrollRangeTovisible, as was suggested in dklt's answer's comments...

ONLY dklt's original solution works for me. The rest doesn't.

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
}
查看更多
SAY GOODBYE
5楼-- · 2019-01-10 19:58

That works for me. Reference: UITextView setText should not jump to top in 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];
}
查看更多
你好瞎i
6楼-- · 2019-01-10 20:00

This is obviously an iOS 7 bug. Here is a workaround until apple fixes it. The workaround is basically instantiates a UITextView by creating an NSTextStorage and NSLayoutManager from scratch. Apple must have forgotten to initialize something in UITextView initialization method. I filed a bug report and I hope you do too.

// 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];
}
查看更多
男人必须洒脱
7楼-- · 2019-01-10 20:00

There are two problems in iOS 7 that could explain your problem:

  • The contentOffset is not always up to date in iOS 7.
  • scrollRangeToVisible: will not scroll to an empty line at the end of the text view.

The solution could be:

-(void)scrollOutputToBottom {
    CGRect caretRect = [textView caretRectForPosition:textView.endOfDocument];
    [textView scrollRectToVisible:caretRect animated:NO];
}
查看更多
登录 后发表回答