Sam Stewart
Sam Stewart

Reputation: 1480

How do I force a UITextView to scroll to the top every time I change the text?

OK, I'm having some problem with the UITextView. Here's the issue:

I add some text to a UITextView. The user then double clicks to select something. I then change the text in the UITextView (programatically as above) and the UITextView scrolls to the bottom of the page where there is a cursor.

However, that is NOT where the user clicked. It ALWAYS scrolls to the bottom of the UITextView regardless of where the user clicked.

So here's my question: How do I force the UITextView to scroll to the top every time I change the text? I've tried contentOffset and scrollRangeToVisible. Neither work.

Any suggestions would be appreciated.

Upvotes: 103

Views: 76546

Answers (30)

Revanth Kausikan
Revanth Kausikan

Reputation: 673

The default behaviour of the textview is the scroll to the bottom, as it enables the users to continue editing.

Setting the textview offset as its inset's top value solves it for me.

override func viewDidLayoutSubviews() {  
    super.viewDidLayoutSubviews()  
    self.myTextView.contentOffset.y = -self.myTextView.contentInset.top  
}

Note: My textView was embedded inside a navigation controller.

Upvotes: 1

Charlie S
Charlie S

Reputation: 4594

I had to call the following inside viewDidLayoutSubviews (calling inside viewDidLoad was too early):

    myTextView.setContentOffset(.zero, animated: true)

Upvotes: 8

Adam
Adam

Reputation: 1913

For Swift I think the cleanest way to do this is to subclass UITextView and use this little trick

import UIKit

class MyTextView: UITextView {
    override var text: String! {
        willSet {
            isScrollEnabled = false
        } didSet {
            isScrollEnabled = true
        }
    }
}

Upvotes: 0

garg
garg

Reputation: 2727

This worked for me

  override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    DispatchQueue.main.async {
      self.textView.scrollRangeToVisible(NSRange(location: 0, length: 0))
    }
  }

Upvotes: 8

Wayne Lo
Wayne Lo

Reputation: 3719

UITextView*note;
[note setContentOffset:CGPointZero animated:YES];

This does it for me.

Swift version (Swift 4.1 with iOS 11 on Xcode 9.3):

note.setContentOffset(.zero, animated: true)

Upvotes: 153

Terry
Terry

Reputation: 339

This code solves the problem and also helps to avoid needless autoscroll to the end of textView. Just use method setTextPreservingContentOffset: instead of setting text to textView directly.

@interface ViewController () {
    __weak IBOutlet UITextView *textView;
    CGPoint lastContentOffset;
    NSTimer *autoscrollTimer;
    BOOL revertAnyContentOffsetChanges;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [textView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)dealloc
{
    [textView removeObserver:self forKeyPath:@"contentOffset"];
}

- (void)setTextPreservingContentOffset:(NSString *)text
{
    lastContentOffset = textView.contentOffset;
    [autoscrollTimer invalidate];
    revertAnyContentOffsetChanges = YES;
    textView.text = text;
    autoscrollTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(enableScrolling:) userInfo:nil repeats:NO];
    autoscrollTimer.tolerance = 1;
}

- (void)enableScrolling:(NSTimer *)timer
{
    revertAnyContentOffsetChanges = NO;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (revertAnyContentOffsetChanges && [keyPath isEqualToString:@"contentOffset"] && [[change valueForKey:@"new"] CGPointValue].y != lastContentOffset.y) {
        [textView setContentOffset:lastContentOffset animated:NO];
    }
}

@end

Upvotes: 1

Maksym Horbenko
Maksym Horbenko

Reputation: 91

That's how I did it

Swift 4:

extension UITextView {

    override open func draw(_ rect: CGRect)
    {
        super.draw(rect)
        setContentOffset(CGPoint.zero, animated: false)
    }

 }

Upvotes: 9

erkancan
erkancan

Reputation: 61

just you can use this code

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    textView.scrollRangeToVisible(NSMakeRange(0, 0))
}

Upvotes: 6

Wayne Henderson
Wayne Henderson

Reputation: 267

The answer by ChallengerGuy fully solved my problem once I added a brief delay before re-enabling scrolling. Prior to adding the delay, my PageController pages were all fixed except for the first page, which would also fix itself after any user interaction with the page. Adding the delay fixed the UITextView scroll position of that first page as well.

override open func viewDidLoad() {
    super.viewDidLoad()
    textView.isScrollEnabled = false
    textView.text = textToScroll            
   ... other stuff
}

override open func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    Central().delay(0.5) {
        self.textView.isScrollEnabled = true
    }
}

(The delay function in my Central() file.)

func delay(_ delay: Double, closure:@escaping ()->()) {
    DispatchQueue.main.asyncAfter(deadline: .now() + delay) {
        closure()
    }
}

Upvotes: 0

user5104628
user5104628

Reputation:

I think the reason why some people are having problems with this issue is because they are trying to set the TextView scroll position, before this TextView has appeared.

There is nothing to scroll, when the TextView hasn't yet appeared.

You need to wait for the TextView to appear first, and only then set its scroll position inside the viewDidAppear method as shown below:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    textView.setContentOffset(CGPoint.zero, animated: false)
}

Upvotes: 1

hawmack13
hawmack13

Reputation: 233

Swift 3 version of drew_s answer earlier, which is the only thing that worked for me after trying most of the other answers.

    override func viewWillAppear(_ animated: Bool) {
    DispatchQueue.main.async{
        let desiredOffset = CGPoint(x: 0, y: -self.person_description.contentInset.top)
        self.person_description.setContentOffset(desiredOffset, animated: false)
    }
}

This was a cut and paste of working code. Replace person_description with your textview variable.

Upvotes: 1

David.Chu.ca
David.Chu.ca

Reputation: 38644

Here are my codes. It works fine.

class MyViewController: ... 
{
  private offsetY: CGFloat = 0
  @IBOutlet weak var textView: UITextView!
  ...

  override viewWillAppear(...) 
  {
      ...
      offsetY = self.textView.contentOffset.y
      ...
  }
  ...
  func refreshView() {
      let offSetY = textView.contentOffset.y
      self.textView.setContentOffset(CGPoint(x:0, y:0), animated: true)
      DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
        [unowned self] in
        self.textView.setContentOffset(CGPoint(x:0, y:self.offSetY), 
           animated: true)
        self.textView.setNeedsDisplay()
      }
  }

Upvotes: 1

AechoLiu
AechoLiu

Reputation: 18368

My issue is to set textView to scroll to top when view appeared. For iOS 10.3.2, and Swift 3.

Solution 1:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    // It doesn't work. Very strange.
    self.textView.setContentOffset(CGPoint.zero, animated: false)
}
override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    // It works, but with an animation effection.
    self.textView.setContentOffset(CGPoint.zero, animated: false)
}

Solution 2:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    // It works.       
    self.textView.contentOffset = CGPoint.zero
}

Upvotes: 6

SaRaVaNaN DM
SaRaVaNaN DM

Reputation: 4410

I had the problem, but in my case textview is the subview of some custom view and added it to ViewController. None of the solution worked for me. To solve the problem added 0.1 sec delay and then enabled scroll. It worked for me.

textView.isScrollEnabled = false
..
textView.text = // set your text here

let dispatchTime = DispatchTime.now() + 0.1
DispatchQueue.main.asyncAfter(deadline: dispatchTime) {
   self.textView.isScrollEnabled = true
}

Upvotes: 2

Marco Leong
Marco Leong

Reputation: 565

In Swift 3:

  • viewWillLayoutSubviews is the place to make changes. viewWillAppear should work as well, but logically, layout should be perform in viewWillLayoutSubviews.

  • Regarding methods, both scrollRangeToVisible and setContentOffset will work. However, setContentOffset allows animation to be off or on.

Assume the UITextView is named as yourUITextView. Here's the code:

// Connects to the TextView in the interface builder.
@IBOutlet weak var yourUITextView: UITextView!

/// Overrides the viewWillLayoutSubviews of the super class.
override func viewWillLayoutSubviews() {

    // Ensures the super class is happy.
    super.viewWillLayoutSubviews()

    // Option 1:
    // Scrolls to a range of text, (0,0) in this very case, animated.
    yourUITextView.scrollRangeToVisible(NSRange(location: 0, length: 0))

    // Option 2:
    // Sets the offset directly, optional animation.
    yourUITextView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
}

Upvotes: 4

Stephen Rowe
Stephen Rowe

Reputation: 29

Problem solved: iOS = 10.2 Swift 3 UITextView

I just used the following line:

displayText.contentOffset.y = 0

Upvotes: 2

Darkwonder
Darkwonder

Reputation: 1305

Solution for Xcode 8.0. Swift 2.3. Works for interface build and can be easily modified to work programmatically.

class MBTextView: UITextView
{
    required init?(coder aDecoder: NSCoder)
    {
        super.init(coder: aDecoder)
        setupView()
    }

    func setupView() {}
}

class MBStartFromTopTV: MBTextView
{
    override func setupView()
    {
        // some custom code
    }

    override func drawRect(rect: CGRect)
    {
        super.drawRect(rect)
        setContentOffset(CGPoint.zero, animated: false)
    }
}

Upvotes: -1

ChallengerGuy
ChallengerGuy

Reputation: 2395

For iOS 10 and Swift 3 I did:

override func viewDidLoad() {
    super.viewDidLoad()
    textview.isScrollEnabled = false
}

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    textView.isScrollEnabled = true
}

Worked for me, not sure if you're having the problem with the latest version of Swift and iOS.

Upvotes: 14

Kahng
Kahng

Reputation: 189

-(void) viewDidAppear:(BOOL)animated{
[mytextView scrollRangeToVisible:NSMakeRange(0,0)];}

- (void)viewWillAppear:(BOOL)animated{
[mytextView scrollRangeToVisible:NSMakeRange(0,0)];}
  • ViewWillappear will do it instantly before user can notice, but ViewDidDisappear section will animate it lazily.
  • [mytextView setContentOffset:CGPointZero animated:YES]; does NOT work sometimes(I don't know why), so use scrollrangetovisible rather.

Upvotes: 3

nerd2know
nerd2know

Reputation: 844

If anyone has this problem in iOS 8, I found that just setting the UITextView's text property, then calling scrollRangeToVisible with an NSRange with location:0, length:0, worked. My text view was not editable, and I tested both selectable and not selectable (neither setting affected the result). Here's a Swift example:

myTextView.text = "Text that is long enough to scroll"
myTextView.scrollRangeToVisible(NSRange(location:0, length:0))

Upvotes: 68

Nikolay Shubenkov
Nikolay Shubenkov

Reputation: 3223

Try to use this 2 lines solution:

view.layoutIfNeeded()
textView.contentOffset = CGPointMake(textView.contentInset.left, textView.contentInset.top)

Upvotes: 1

Paul T.
Paul T.

Reputation: 5038

For me in iOS 9 it stopped to work for me with attributed string with method scrollRangeToVisible (the 1 row was with bold font, other rows were with regular font)

So I had to use:

textViewMain.attributedText = attributedString
textViewMain.scrollRangeToVisible(NSRange(location:0, length:0))
delay(0.0, closure: { 
                self.textViewMain.scrollRectToVisible(CGRectMake(0, 0, 10, 10), animated: false)
            })

where delay is:

func delay(delay:Double, closure:()->Void) {
    dispatch_after(
        dispatch_time(
            DISPATCH_TIME_NOW,
            Int64(delay * Double(NSEC_PER_SEC))
        ),
        dispatch_get_main_queue(), closure)
}

Upvotes: 1

Albert  Zou
Albert Zou

Reputation: 165

-(void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];

    [textview setContentOffset:CGPointZero animated:NO];
}

Upvotes: 3

glace
glace

Reputation: 2210

Swift 2 Answer:

textView.scrollEnabled = false

/* Set the content of your textView here */

textView.scrollEnabled = true

This prevents the textView from scrolling to the end of the text after setting it.

Upvotes: 4

Igor
Igor

Reputation: 12303

For me fine works this code:

    textView.attributedText = newText //or textView.text = ...

    //this part of code scrolls to top
    textView.contentOffset.y = -64
    textView.scrollEnabled = false
    textView.layoutIfNeeded() //if don't work, try to delete this line
    textView.scrollEnabled = true

For scroll to exact position and show it on top of screen I use this code:

    var scrollToLocation = 50 //<needed position>
    textView.contentOffset.y = textView.contentSize.height
    textView.scrollRangeToVisible(NSRange.init(location: scrollToLocation, length: 1))

Setting contentOffset.y scrolls to the end of text, and then scrollRangeToVisible scrolls up to value of scrollToLocation. Thereby, needed position appears in first line of scrollView.

Upvotes: 1

Drew S.
Drew S.

Reputation: 526

Since none of these solutions worked for me and I wasted way too much time piecing together solutions, this finally solved it for me.

override func viewWillAppear(animated: Bool) {
    dispatch_async(dispatch_get_main_queue(), {
        let desiredOffset = CGPoint(x: 0, y: -self.textView.contentInset.top)
        self.textView.setContentOffset(desiredOffset, animated: false)
    })
}

This is really silly that this is not default behavior for this control.

I hope this helps someone else out.

Upvotes: 19

Hermann Klecker
Hermann Klecker

Reputation: 14068

This worked for me. It is based on RyanTCBs answer but it is the Objective-C variant of the same solution:

- (void) viewWillAppear:(BOOL)animated {
    // For some reason the text view, which is a scroll view, did scroll to the end of the text which seems to hide the imprint etc at the beginning of the text.
    // On some devices it is not obvious that there is more text when the user scrolls up.
    // Therefore we need to scroll textView to the top.
    [self.textView scrollRangeToVisible:NSMakeRange(0, 0)];
}

Upvotes: 3

Patricia
Patricia

Reputation: 289

[txtView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];

This line of code works for me.

Upvotes: 5

MacInnis
MacInnis

Reputation: 758

I have an updated answer which will have the textview appear properly, and without the user experiencing a scroll animation.

override func viewWillAppear(animated: Bool) {
    dispatch_async(dispatch_get_main_queue(), {
        self.introductionText.scrollRangeToVisible(NSMakeRange(0, 0))
    })

Upvotes: 10

ageorgios
ageorgios

Reputation: 323

This is how it worked on iOS 9 Release so as the textView is scrolled on top before appearing on screen

- (void)viewDidLoad
{
    [super viewDidLoad];
    textView.scrollEnabled = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    textView.scrollEnabled = YES;
}

Upvotes: 10

Related Questions