UITextView setText should not jump to top in ios8
Asked Answered
P

3

5

Following iOS 8 code is called every second:

- (void)appendString(NSString *)newString toTextView:(UITextView *)textView {
    textView.scrollEnabled = NO;
    textView.text = [NSString stringWithFormat:@"%@%@%@", textView.text, newString, @"\n"];
    textView.scrollEnabled = YES;
    [textView scrollRangeToVisible:NSMakeRange(textView.text.length, 0)];
}

The goal is to have the same scrolling down behaviour as the XCode console when the text starts running off the bottom. Unfortunately, setText causes the view to reset to the top before I can scroll down again with scrollRangeToVisible.

This was solved in iOS7 with the above code and it worked, but after upgrading last week to iOS8, that solution no longer seems to work anymore.

I can't figure out how to get this going fluently without the jumping behaviour?

Pilotage answered 25/9, 2014 at 11:58 Comment(0)
M
-2

Try just to add text to UITextView (without scrollRangeToVisible/scrollEnabled). It seams that hack with scroll enabled/disabled is no more needed in iOS8 SDK. UITextView scrolls automatically.

Mutton answered 10/10, 2014 at 15:9 Comment(2)
Thanks! It's been a while since I needed this for testing purposes, and since then it's been removed from the project. I do remember though that it was something obscure like this that solved it though. Accepted as correct answer for now ;-)Pilotage
This is not true, the scroll is not automatic for me.Remonstrant
B
28

I meet this problem too. You can try this.

textView.layoutManager.allowsNonContiguousLayout = NO;

refrence:http://hayatomo.com/2014/09/26/1307

Bromide answered 18/3, 2015 at 13:45 Comment(4)
wow..i have been solving this for a long long time already..Thanks for your answer.Irreproachable
I found this after working on this problem for hours. This is the right answer.Crassus
Dude, you're a genius!Miyokomizar
This is the RIGHT answer. Specifically, after setting allowsNonContiguousLayout = NO, you then change the text in the text view and then scroll. _consoleView.text = text; [_consoleView scrollRangeToVisible:NSMakeRange(_consoleView.text.length - 1, 1)];Remonstrant
W
3

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];
}];
Wrung answered 7/1, 2015 at 4:34 Comment(1)
If I'm using scrollEnabled = NO and scrollEnabled = YES then its working on iOS 9 but not on iOS 8 and the only solution that work's for me is setting content offset in NSOperationQueue only. Deserve up-voting for the answer.Superorganic
M
-2

Try just to add text to UITextView (without scrollRangeToVisible/scrollEnabled). It seams that hack with scroll enabled/disabled is no more needed in iOS8 SDK. UITextView scrolls automatically.

Mutton answered 10/10, 2014 at 15:9 Comment(2)
Thanks! It's been a while since I needed this for testing purposes, and since then it's been removed from the project. I do remember though that it was something obscure like this that solved it though. Accepted as correct answer for now ;-)Pilotage
This is not true, the scroll is not automatic for me.Remonstrant

© 2022 - 2024 — McMap. All rights reserved.