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.
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:
This is how it looks after manually scroll up to the top.
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)];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With