UITextView starts at Bottom or Middle of the text

36,729

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
    }
}
Share:
36,729
Alex Wulff
Author by

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, 2020

Comments

  • Alex Wulff
    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.

    enter image description here

    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
    Alex Wulff over 9 years
    I 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
    Alex Wulff over 9 years
    No 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
    Mrunal over 9 years
    Try commenting automaticallyAdjustsScrollViewInsets line and check.
  • mikemike396
    mikemike396 about 9 years
    Works great! I still don't understand why we have to do this. Why doesn't it auto start at 0,0?
  • user2700551
    user2700551 almost 9 years
    I 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
    BlueGuy over 8 years
    This is exactly what I needed to fix this issue. Thank you!
  • devios1
    devios1 over 8 years
    I find scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO works better as it avoids an initial rendering caused by the implicit animation.
  • zeeple
    zeeple over 8 years
    I do not see where the Adjust Scroll View Insets exists within the Attributes Inspector.
  • keshav vishwkarma
    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
    Rodrigo Pinto over 8 years
    Maybe because I am using nibs, this is the only one that worked for me.
  • Patrick
    Patrick over 8 years
    @RodrigoPedroso Using storyboard and this was the only one that worked for me as well
  • Form
    Form over 8 years
    Same 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
    Miguel Ribeiro over 8 years
    I'm using storyboard. Could this be related to iOS version?
  • Adrian
    Adrian over 8 years
    I tried it in viewDidLayoutSubviews() and I saw the textView scrolling around when the ViewController loaded, despite animation set to false. I resolved it by putting setContentOffset in viewWillAppear w/ animation set to false.
  • JSmyth
    JSmyth about 8 years
    Worked for me perfectly in Xcode 7.3 (7D175) / iOS 9.3
  • Craig Otis
    Craig Otis about 8 years
    This also worked for me (where the accepted answer did not) when using storyboards.
  • mshrestha
    mshrestha almost 8 years
    This 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
    zeeple almost 8 years
    Hi 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
    Mr. Xcoder over 7 years
    Only the alternative solution gets the job done, the first one is not working... Why is that?
  • Sami
    Sami over 7 years
    Wow. I can't believe that was my issue. What is the benefit to having to do it like this?
  • nivritgupta
    nivritgupta over 6 years
    CGPointZero is unavailable in newer version of swift use textView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
  • Chandni
    Chandni over 6 years
    Thank you it's working for me :) but why this issue occur can you please explain.
  • Anna Harrison
    Anna Harrison over 4 years
    setting 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