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
Try to place your code in viewDidLayoutSubviews
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.
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:
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With