How to make UITextView scroll up when entering it?
Hi,
I have a UITextView included in a UITableViewCell. The layout is correct when the views are initially displayed, but once I click in the UITextView it DOES NOT automatically scrolls up and the keyboard stays in front of the UITextView so that I can't see what I am typing. I have to scroll the view manually to see what I am typing.
This image is when the UITextView is not active:
And this one is when I clicked in the UITextView to make it active:
I want the cell with the UITextView to scroll up. How can I achieve this?
Thanks for your reply musicwind95. It actually worked, but it works only for ONE case, that means, no matter which UITextView I click in, the view scrolls to the specific given point (in your example: (0, 480, 320, 60)). But I have many differents UITextViews on my table, and I want bring the view to scroll exactly at the bottom of a specific UITableView (that I clicked in) dynamically.
What would also work is to bring the cell with the UITextView to scroll to top.
Thanks for your reply musicwind95. It actually worked, but it works only for ONE case, that means, no matter which UITextView I click in, the view scrolls to the specific given point (in your example: (0, 480, 320, 60)). But I have many differents UITextViews on my table, and I want bring the view to scroll exactly at the bottom of a specific UITableView (that I clicked in) dynamically.
What would also work is to bring the cell with the UITextView to scroll to top.
I would appreciate any additional suggestions.
Thanks again for answering.
That CGRect was a very rough approximation; I just wanted to test the scrolling. You'll need to use tableView:didSelectRowAtIndexPath:, determine the section and row (using indexPath.section and indexPath.row) of the cell you've tapped on, and then set a CGRect according to the cell's current position. Figuring out that location is beyond me—I'm still working on counting it out, although there's probably a method that allows you to get the frame of the current cell. Check the docs.
Or, if you're happy with just counting, then use IB's third inspector (select something and press Command-3) to get the frame and location of items.
Hi musicwind97, I've already done it. My solution is not the best but it works very well: everything happens on tableView:cellForRowAtIndexPath:
1) For every cell that has a UITextView I have a NSDictionary: I save all my UITextView's on the cells on NSDictionary with a particular key.
2) And I also have another NSDictionary with the indexPath objects that are created in tableView:cellForRowAtIndexPath. If a cell A has UITextView A, they will have the same key in the NSDictionaries.
3) I implement the UITextViewDelegate and call textViewDidBeginEditing: here is my code:
- (void)textFieldDidBeginEditing: (UITextField *)textField {
NSArray *keys = [self.answersDic allKeysForObject:textField];
id *key = (id *)[keys objectAtIndex:0];
NSIndexPath *tempIndex=[self.indexPathForViews objectForKey:key];
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
OBSERVATIONS:
a) answersDic is the NSDictionary where my UITextViews are saved.
b) indexPathForViews is the NSDictionary where my IndexPaths are saved.
c) the most important part of this code: the method, that brings the view to scroll up:
[tableViewLocal scrollToRowAtIndexPath:tempIndex atScrollPosition:UITableViewScrollPositionTop animated:YES];