Tuesday, January 31, 2012

Show the UITableView textField cell above the Keyboard when typing

Scenario:
Keyboard
Keyboard
I have an app where, in Interface Builder, I set up a UITableView that has a text field near the bottom of the view. When I run the app and try to enter text into that field, the keyboard slides up overtop of the field so I can’t see what I’m typing until I hide the keyboard again.
Most of the iPhone App developers come across this situation. I found the solution for this issue.

Solution:

- (UITableViewCell *)tableView:(UITableView *)mytableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell.delegate = self;
cell.indexPath = indexPath;
cell.rightTextField.tag = (NSInteger)indexPath;
//Disables UITableViewCell from accidentally becoming selected.
cell.selectionStyle = UITableViewCellEditingStyleNone;
}

– (void)keyboardWasShown:(NSNotification*)aNotification
{
//when keyboard is up, that time just bring your text filed above the keyboard
NSIndexPath *indexPath = (NSIndexPath*)self.actifText.tag;
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.m_myTable.contentInset = contentInsets;
self.m_myTable.scrollIndicatorInsets = contentInsets;
[self.m_myTable scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];

// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{ // Once keyboard is hidden then bring back your table into your original position.
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
self.m_myTable.contentInset = contentInsets;
self.m_myTable.scrollIndicatorInsets = contentInsets;

No comments:

Post a Comment