soulshined
soulshined

Reputation: 10622

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

Upvotes: 6

Views: 3921

Answers (3)

Kenny Bambridge
Kenny Bambridge

Reputation: 407

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:.

Upvotes: 1

tuledev
tuledev

Reputation: 10327

Try to place your code in viewDidLayoutSubviews

Upvotes: 17

DFedor
DFedor

Reputation: 1048

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.

Upvotes: 5

Related Questions