Yuchen
Yuchen

Reputation: 33126

UITextView is partially scrolled down when view opened

I create a UITextView and add a lot of text to it. When I launch the view, the UITextView is always partially scrolled down. Of cause the user can scroll up, but it just feels weird.

What happens? Did I miss any obvious settings?

Please refer to the following image. Part of text is cut out. I borrow the text from link.

enter image description here

I started a simple Single View Application. This is how my viewDidLoad looks like:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"license"
                                                     ofType:@"txt"];
    NSString* terms = [NSString stringWithContentsOfFile:path
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
    self.textField.text = terms;
}

And this is my storyboard:

enter image description here

This is how it looks after manually scroll up to the top.

enter image description here

Upvotes: 9

Views: 1033

Answers (5)

Panayot
Panayot

Reputation: 554

The best working solution for me (Objective C):

- (void)viewDidLayoutSubviews {
     [self.myTextView setContentOffset:CGPointZero animated:NO];
}

Upvotes: -1

xleon
xleon

Reputation: 6375

For those using Xamarin, this is the only solution that works for me:

textView.Text = "very long text";

DispatchQueue.MainQueue.DispatchAsync(() =>
{
    textView.ContentOffset = new CGPoint(0, 0);
});

Upvotes: 1

Yuchen
Yuchen

Reputation: 33126

This used to happens on all iPhone/iPod devices under landscape mode, and solutions by the others will work. Now it only happens for iPhone 6+ or iPhone 6s+ under landscape mode. This is a work around for it:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.textView scrollRangeToVisible:NSMakeRange(0, 1)];
    });
}

The view is scrolled somehow after the viewWillAppear and before viewDidAppear, and that's why we need the dispatch above.

Upvotes: 3

luna_
luna_

Reputation: 312

Add this line at the end of your viewDidLoad:

[self.textField scrollRangeToVisible:NSMakeRange(0, 1)];

Like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"license"
                                                     ofType:@"txt"];
    NSString* terms = [NSString stringWithContentsOfFile:path
                                                encoding:NSUTF8StringEncoding
                                                   error:nil];
    self.textField.text = terms;
    [self.textField scrollRangeToVisible:NSMakeRange(0, 1)];
}

Upvotes: 5

AliNadi
AliNadi

Reputation: 500

I had the same problem and I solved it by getting the previous scroll position and reseting it after updating the "text"

CGPoint p = [self.texField contentOffset];
self.textField.text = terms;
[self.textField setContentOffset:p animated:NO];
[self.textField scrollRangeToVisible:NSMakeRange([tv.text length], 0)];

Upvotes: 2

Related Questions