简体   繁体   中英

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

尝试将您的代码放在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: .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM