Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 613
Yuchen Avatar asked Apr 09 '15 19:04

Yuchen


2 Answers

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)];
}
like image 77
luna_ Avatar answered Nov 04 '22 11:11

luna_


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.

like image 3
Yuchen Avatar answered Nov 04 '22 13:11

Yuchen