Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9 scroll UITextView to top

In iOS 7 and/or 8, scrolling my UITextView to the top programmatically works as intended in viewDidLoad:

[self.someTextView scrollRangeToVisible:NSMakeRange(0, 0)];

Now it just loads to my some other frame. My guess is it's loading to my contentOffset UIEdgeInsetsMake(0, 0, 65, 0)

How do I make this work >=iOS9?

Note: I've already tried placing in viewDidAppear with no results


EDIT The issue occurs in circumstances when my UITextView text is greater than the height of it's view. I use UIEdgeInsets because the view is presented modally, and is somewhat larger than the screen bounds. However, the scrollRangeToVisible still works iOS 7/8 but not iOS9, in this circumstance

like image 324
soulshined Avatar asked Aug 26 '15 23:08

soulshined


3 Answers

Try to place your code in viewDidLayoutSubviews

like image 106
tuledev Avatar answered Nov 17 '22 00:11

tuledev


I had the same problem on iOS 9, and this workaround fixed it:

@property (nonatomic) bool workaroundIOS9Bug;

- (void)viewDidLoad {
  self.workaroundIOS9Bug = [[UIDevice currentDevice].systemVersion floatValue] >= 9.0;
}


-(void)viewDidLayoutSubviews {
  if (self.workaroundIOS9Bug) {
    self.textView.contentOffset = CGPointZero;
    self.workaroundIOS9Bug = false;
  }
}

Meaning, it's only necessary to do this extra clearing of contentOffset the first time that subviews are being laid out. Doing it all the time would be a pain when the user rotates the screen or is doing other stuff with the view.

like image 25
DFedor Avatar answered Nov 17 '22 00:11

DFedor


Have you tried using

[self.someTextView scrollRectToVisible:CGRectMake(0, 0, x, x) animated:YES]

where x is some nonzero width? It seems that scrollRectToVisible: scrolls farther up than scrollRangeToVisible:.

like image 1
Kenny Bambridge Avatar answered Nov 17 '22 00:11

Kenny Bambridge