Aamir
Aamir

Reputation: 16957

UITextView always appears scrolled to bottom of content?

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.

Upvotes: 3

Views: 1623

Answers (3)

Ramesh_T
Ramesh_T

Reputation: 1261

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];

Upvotes: 0

Saranjith
Saranjith

Reputation: 11567

Swift 4

contentTextView.scrollRangeToVisible(NSRange(location: 0, length: 0))

Upvotes: 1

Ronak Chaniyara
Ronak Chaniyara

Reputation: 5435

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)];

Upvotes: 4

Related Questions