UITextView starts at Bottom or Middle of the text
Solution 1
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
}
Solution 2
All of the answers above did not work for me. However, the secret turns out to be to implement your solution within an override of viewDidLayoutSubviews, as in:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
welcomeText.contentOffset = .zero
}
HTH :)
Solution 3
In Swift 2
You can use this to make the textView start from the top:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
myTextView.setContentOffset(CGPointZero, animated: false)
}
Confirmed working in Xcode 7.2 with Swift 2
Solution 4
Try this below code -
if ( [self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]){
self.automaticallyAdjustsScrollViewInsets = NO;
}
Or you can also set this property by StoryBoard -
Select ViewController then select attributes inspector now unchecked Adjust Scroll View Insets
.
Solution 5
For Swift >2.2, I had issues with iOS 8 and iOS 9 using above methods as there are no single answer that works so here is what I did to make it work for both.
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 9.0, *) {
textView.scrollEnabled = false
}
self.textView.scrollRangeToVisible(NSMakeRange(0, 0))
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if #available(iOS 9.0, *) {
textView.scrollEnabled = true
}
}
Alex Wulff
I'm a student app developer and Arduino hobbyist. Check out www.ConiferApps.com and www.AlexWulff.com to see some of the things that I've done.
Updated on June 28, 2020Comments
-
Alex Wulff almost 4 years
I'll get right to it. I have a
UItextView
placed in my view that when needs to scroll to see all the text (when a lot of text is present in the textView) the textView starts in the middle of the text sometimes and the bottom of the text other times.Editing is not enabled on the textView. I need a way to force the textView to start at the top, every time. I saw some questions somewhat like this where other people used a content offset, but I do not really know how that works or if it would even be applicable here.
Thanks for your help.
-
Alex Wulff over 9 yearsI tried placing
[self.scrollView setContentOffset:CGPointMake(0, -self.scrollView.contentInset.top) animated:YES];
in multiple places: after where I set the code programmatically, in the viewDidLoad, and in the viewWillAppear, but no lucK. I was thinking it had something to do with my navigation bar? -
Alex Wulff over 9 yearsNo luck on this one either... the text view still is scrolled all the way to the bottom. Could it be having something to do with the automaticallyAdjustsScrollViewInsets property? Because I have the textview placed beneath the navigationbar with this enabled, so the navigation bar does not eclipse the textview. Thanks for the answer.
-
Mrunal over 9 yearsTry commenting automaticallyAdjustsScrollViewInsets line and check.
-
mikemike396 about 9 yearsWorks great! I still don't understand why we have to do this. Why doesn't it auto start at 0,0?
-
user2700551 almost 9 yearsI think that because it's meant to be a user input area, when you type text, your cursor is located at the end of the text, and if you leave the screen, you want to continue typing from where you left when you come back.
-
BlueGuy over 8 yearsThis is exactly what I needed to fix this issue. Thank you!
-
devios1 over 8 yearsI find
scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO
works better as it avoids an initial rendering caused by the implicit animation. -
zeeple over 8 yearsI do not see where the Adjust Scroll View Insets exists within the Attributes Inspector.
-
keshav vishwkarma over 8 years@zeeple It's a ViewController property that will display below the title of the ViewController in Attributes Inspector after selecting ViewController in storyboard. Please check properly I hope you will find it.
-
Rodrigo Pinto over 8 yearsMaybe because I am using nibs, this is the only one that worked for me.
-
Patrick over 8 years@RodrigoPedroso Using storyboard and this was the only one that worked for me as well
-
Form over 8 yearsSame here, this is the only solution that worked for me. My text view is in a nib, for the record. Don't know if that changes anything.
-
Miguel Ribeiro over 8 yearsI'm using storyboard. Could this be related to iOS version?
-
Adrian over 8 yearsI tried it in
viewDidLayoutSubviews()
and I saw the textView scrolling around when the ViewController loaded, despite animation set to false. I resolved it by puttingsetContentOffset
inviewWillAppear
w/ animation set to false. -
JSmyth about 8 yearsWorked for me perfectly in Xcode 7.3 (7D175) / iOS 9.3
-
Craig Otis about 8 yearsThis also worked for me (where the accepted answer did not) when using storyboards.
-
mshrestha almost 8 yearsThis is definitely not working for me. The answer provided by @zeeple is working but if we did use the answer the textview scrolls up on any action performed on the view. Would appreciate some help.
-
zeeple almost 8 yearsHi Manjul, I am not seeing the same behavior in my implementation. I can touch in the view, wiggle it some, etc and it does not auto-scroll to the top.
-
Mr. Xcoder over 7 yearsOnly the alternative solution gets the job done, the first one is not working... Why is that?
-
Sami over 7 yearsWow. I can't believe that was my issue. What is the benefit to having to do it like this?
-
nivritgupta over 6 yearsCGPointZero is unavailable in newer version of swift use textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
-
Chandni over 6 yearsThank you it's working for me :) but why this issue occur can you please explain.
-
Anna Harrison over 4 yearssetting the textview scrollEnabled to false and then true is what worked for me. And I didn't have to put it into viewDidLoad or ViewDidLayoutSubviews. I put the scrollEnabled = false right before we set the text and then I put the scrollEnable = true in a completion handler for an animation that makes the texView appear