I'm attempting to adjust a scroll view when a user taps the keyboard's "Next" button. The tricky part is that I'm programmatically generating custom table cells. Each cell holds a text field. It looks something like this:
I tried following Apple's instructions for moving the content from behind the keyboard, but I found that it doesn't quite work for this instance, primarily because the UIKeyboardDidShowNotification is only called once, not for each time the "Next" button is tapped.
My solution was to rewire Apple's example to use UIKeyboardDidShowNotification to store the keyboard height and textFieldDidBeginEditing to handle the scroll view adjustment.
Here's how I'm (attempting) to adjust the scroll view:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbHeight, 0.0);
scrollView.contentInset = contentInsets;
scrollView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbHeight;
if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbHeight);
[scrollView setContentOffset:scrollPoint animated:YES];
}
}
In this function, the if block never fires. I used a couple of NSLog statements to find that each instance of activeField reports the same origin (despite not sharing a visible origin when the application is running).
I know that the activeField variable is updating properly, as I've dumped it to the log, and the field's tag is different every time this function is called.
S开发者_开发百科o, my question--I think--is: how do I get programmatically generated, custom table cells that contain a text field to have the correct origin?
A tableView is a scrollview, I think you should not introduce a new level of complexity by wrapping the tableView in a scrollView.
To get the TableView scroll to a certein cell you can use: scrollToRowAtIndexPath:atScrollPosition:animated:
.
If the keyboard hides the bottom rows which need to be scroll into sight, just reduce the tableView's height. You can get the keyboard's frame in the userInfo dictionary passed by delegate calls and notifications.
Subview's have their frame
set based on their superviews. So within each cell the textField
is at the same location. So you are getting the same origin for each of your text fields. What you want here is the cell's frame. Assuming that the cell is the parent of the textField
. You can do this activeField.superview.frame
instead of activeField.frame
.
精彩评论