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
Swift - Must Call a Designated Initializer of the Superclass Skspritenode Error
Getting a Thumbnail of a *.Mov Video iOS
Dragging Scnnode in Arkit Using Scenekit
Storyboard Navigation Controller and Tab Bar Controller
A Server with the Specified Hostname Could Not Be Found
Ios11 Causing Cors Issues in All Mobile Browsers
Firebase Retrieving Data in Swift
Ios: Does Force Quitting the App Disables Background Upload Using Nsurlsession
How to Draw Inside the Black Edges in iOS Sdk with Opengl Es
Detect Whether a Font Is Bold/Italic on iOS
Installed App from Testflight Crashes Due to Alleged Uisearchdisplaycontroller
Sqlite Database on Phonegap/iOS - More Than 5Mb Possible
Objective-C Check If Subviews of Rotated Uiviews Intersect
Delete a Device from My Developer Account to Increase the Count
How to Represent Core Data Optional Scalars (Bool/Int/Double/Float) in Swift
Override a Method in Objective C via Category
Opening Word,Excel, and PDF Files Without Using Uiwebview on iOS
Using Apple's Reachability to Check Remote Server Reachability in Swift