UITextView setText should not jump to top in ios8

前端 未结 3 1980
青春惊慌失措
青春惊慌失措 2021-01-03 04:24

Following iOS 8 code is called every second:

- (void)appendString(NSString *)newString toTextView:(UITextView *)textView {
    textView.scrollEnabled = NO;
          


        
3条回答
  •  醉梦人生
    2021-01-03 05:02

    The following two solutions don't work for me on iOS 8.0.

    textView.scrollEnabled = NO;
    [textView.setText: text];
    textView.scrollEnabled = YES;
    

    and

    CGPoint offset = textView.contentOffset;
    [textView.setText: text];
    [textView setContentOffset:offset];
    

    I setup a delegate to the textview to monitor the scroll event, and noticed that after my operation to restore the offset, the offset is reset to 0 again. So I instead use the main operation queue to make sure my restore operation happens after the "reset to 0" option.

    Here's my solution that works for iOS 8.0.

    CGPoint offset = self.textView.contentOffset;
    self.textView.attributedText = replace;
    [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
        [self.textView setContentOffset: offset];
    }];
    

提交回复
热议问题