3

I am using storyboard with auto layout, scene contains UITextView with some text in it, you can see in following image:

enter image description here

But whenever i move to this scene in running application, it would be like following:

enter image description here
I have researched it a lot and tried following code, even then it not worked for me:

[self.scrollView setContentOffset:CGPointZero animated:NO];

Any help would be appreciated.

Aamir
  • 16,329
  • 10
  • 59
  • 65
  • Possible duplicate of [UITextView Starts at Bottom or middle of text](https://stackoverflow.com/questions/27622437/uitextview-starts-at-bottom-or-middle-of-text) – Saranjith May 30 '18 at 05:23

3 Answers3

4

Try following, may it help:

[yourTextView scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];

OR

set the content offset in viewDidLayoutSubviews for it to take effect.

- (void)viewDidLayoutSubviews {
[yourTextView setContentOffset:CGPointZero animated:NO];
}

OR

in viewDidLoad

[yourTextView scrollRangeToVisible:NSMakeRange(0, 1)];
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
1

Swift 4

contentTextView.scrollRangeToVisible(NSRange(location: 0, length: 0))
Saranjith
  • 11,242
  • 5
  • 69
  • 122
  • If you want to avoid your textview scrolling its content up it's better to use the scrollRectToVisible method. `self.textView.scrollRectToVisible(CGRect.init(x: 0, y: 0, width: 1, height: 1), animated: false)` – Rory Prior Jun 12 '19 at 22:41
0

Here's what worked: just setting the UITextView's text property, then calling scrollRangeToVisible with an NSRange with location:0, length:0. My text view was not editable, and I tested both selectable and non-selectable (neither setting affected the result).

Code:

[textview scrollRectToVisible:NSRange(0, 0) animated:YES];
jkdev
  • 11,360
  • 15
  • 54
  • 77
Ramesh_T
  • 1,251
  • 8
  • 19