如何强制 UITextView 在每次更改文本时滚动到顶部?

好的,我的 UITextView出了点问题。问题是:

我添加一些文本到 UITextView。然后用户双击选择一些东西。 然后改变 UITextView中的文本(如上所示) ,然后 UITextView滚动到页面底部有光标的地方。

但是,这不是用户单击的位置。它总是滚动到底部的 UITextView,无论用户点击哪里。

因此,我的问题是: 如何强制 UITextView在每次更改文本时滚动到顶部?我试过 contentOffsetscrollRangeToVisible。都不工作。

如有任何建议将不胜感激。

74216 次浏览

Try this to move the cursor to the top of the text.

NSRange r  = {0,0};
[yourTextView setSelectedRange:r];

See how that goes. Make sure you call this after all your events have fired or what ever you are doing is done.

I'm not sure if I understand your question, but are you trying to simply scroll the view to the top? If so you should do

[textview scrollRectToVisible:CGRectMake(0,0,1,1) animated:YES];

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)
[txtView setContentOffset:CGPointMake(0.0, 0.0) animated:YES];

This line of code works for me.

I was using attributedString in HTML with text view not editable. Setting the content offset did not work for me either. This worked for me: disable scroll enabled, set the text and then enable the scrolling again

[yourTextView setScrollEnabled:NO];
yourTextView.text = yourtext;
[yourTextView setScrollEnabled:YES];

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

Combined the previous answers, now it should work:

talePageText.scrollEnabled = false
talePageText.textColor = UIColor.blackColor()
talePageText.font = UIFont(name: "Bradley Hand", size: 24.0)
talePageText.contentOffset = CGPointZero
talePageText.scrollRangeToVisible(NSRange(location:0, length:0))
talePageText.scrollEnabled = true

Calling

scrollRangeToVisible(NSMakeRange(0, 0))

works but call it in

override func viewDidAppear(animated: Bool)
[self.textView scrollRectToVisible:CGRectMake(0, 0, 320, self.textView.frame.size.height) animated:NO];

And here is my solution...

    override func viewDidLoad() {
textView.scrollEnabled = false
textView.text = "your text"
}


override func viewDidAppear(animated: Bool) {
textView.scrollEnabled = true
}

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;
}

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))
})

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.

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

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.

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.

-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];


[textview setContentOffset:CGPointZero animated:NO];
}

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)
}

Try to use this 2 lines solution:

view.layoutIfNeeded()
textView.contentOffset = CGPointMake(textView.contentInset.left, textView.contentInset.top)
-(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.

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.

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)
}
}

Problem solved: iOS = 10.2 Swift 3 UITextView

I just used the following line:

displayText.contentOffset.y = 0

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)
}

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
}

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
}

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()
}
}

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.

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)
}

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()
}
}

just you can use this code

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

That's how I did it

Swift 4:

extension UITextView {


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


}

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

This worked for me

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


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

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
}
}
}

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

    myTextView.setContentOffset(.zero, animated: true)

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.