Scroll Uitextview to Bottom

Scroll UITextView To Bottom

You can use the following code if you are talking about UITextView:

-(void)scrollTextViewToBottom:(UITextView *)textView {
if(textView.text.length > 0 ) {
NSRange bottom = NSMakeRange(textView.text.length -1, 1);
[textView scrollRangeToVisible:bottom];
}

}

SWIFT 4:

func scrollTextViewToBottom(textView: UITextView) {
if textView.text.count > 0 {
let location = textView.text.count - 1
let bottom = NSMakeRange(location, 1)
textView.scrollRangeToVisible(bottom)
}
}

Text in UITextView Auto-Scrolled to Bottom

There's a couple ways I know of. Both ways are implemented programmatically through the viewDidLayoutSubviews() method in your view controller. After the call to super.viewDidLayoutSubviews(), you could add:

myTextView.scrollRangeToVisible(NSMakeRange(0, 1))

This would automatically scroll the textView to the first character in the textView. That however might add some unwanted animation when the view appears. The second way would be by adding:

myTextView.setContentOffset(CGPoint.zero, animated: false)

This scrolls the UITextView to point zero (the beginning) and gives you control over whether you want it animated or not.

How to know textView is scroll to bottom?

From this question

You can use this function:

func scrollViewDidScroll(_ scrollView: UIScrollView) {

if (scrollView.contentOffset.y >= scrollView.contentSize.height - scrollView.frame.size.height) {

print( "View scrolled to the bottom" )

}
}

UITextView starts at Bottom or Middle of the text

That did the trick for me!

Objective C:

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

Swift:

self.textView.scrollRangeToVisible(NSMakeRange(0, 0))

Swift 2 (Alternate Solution)

Add this override method to your ViewController

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
textView.setContentOffset(CGPointZero, animated: false)
}

Swift 3 & 4 (syntax edit)

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()

textView.contentOffset = .zero
}

UITextView always appears scrolled to bottom of content?

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


Related Topics



Leave a reply



Submit