Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UITextView text content doesn't start from the top

I have a long text coming from my JSON file but when I click the link from my UITableViewCell to go to my UIViewController page, the UITextView text loads the string content but it does not show the content from the beginning and I have to scroll up all the time.

What I need to do?

like image 277
Ornilo Avatar asked Nov 10 '14 02:11

Ornilo


4 Answers

I had the same problem, and turns out I had to set the content offset in viewDidLayoutSubviews for it to take effect. I'm using this code to display attributed static text.

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

SWIFT 3:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    self.textView.setContentOffset(CGPoint.zero, animated: false)
}
like image 111
iosjillian Avatar answered Nov 19 '22 00:11

iosjillian


This is the only way that worked for me. I disable the scroll of the UITextView before the view is loaded and then i enable it again:

  override func viewWillAppear(_ animated: Bool) {
        yourTextView.isScrollEnabled = false
    }

    override func viewDidAppear(_ animated: Bool) {
        yourTextView.isScrollEnabled = true
    }
like image 41
DanielZanchi Avatar answered Nov 19 '22 01:11

DanielZanchi


[self.textView scrollRangeToVisible:NSMakeRange(0, 1)];

in viewDidLoad

like image 27
Liuk Smith Avatar answered Nov 18 '22 23:11

Liuk Smith


By Programmatically before loading the content disable the scrolling property of textview textview.scrollenabled = NO;

And after loading enable the scrolling of textview textview.scrollenabled = YES;

As well check the XIB, always non-check the scrolling enabled of Textview.

like image 19
Subathra D Avatar answered Nov 19 '22 01:11

Subathra D