Question

I have an application with a UITableView that has UITextFields inside UITableViewCells which the user will have either the virtual UIKeyboard or a Bluetooth keyboard connected to enter text into the UITextFields.

If the Bluetooth keyboard is connected I would like to keep the UITableView full height when selecting and entering text. When there is no Bluetooth keyboard connected if its visible I would like to reduce the UITableview to fit with the UIkeyboard is showing, if its not showing then I would like to make the UITableView full size again.

I have tried to do this by using the UIKeyboard delegate methods

- (void)keyboardDidShow:(NSNotification *)aNotification;
- (void)keyboardDidHide:(NSNotification *)aNotification;

For some reason keyboardDidShow is not accessed ever but keyboardDidHide is accessed when the UIKeyboard is removed from view and I cannot figure out why.

Was it helpful?

Solution

- (void)keyboardDidShow:(NSNotification *)aNotification;
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.3f];
    self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - 255); // or -216
    [UIView commitAnimations];
}

- (void)keyboardDidHide:(NSNotification *)aNotification;
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:0.2f];
    self.tblView.frame = CGRectMake(0, height, self.view.frame.size.width, self.view.frame.size.height - height);
    [UIView commitAnimations];
}

And in UITextField Delegate Methods

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{   
    CGRect rc = [textField bounds];
    rc = [textField convertRect:rc toView:self.tblView];
    CGPoint pt = rc.origin;
    pt.x = 0;
    if(rc.origin.y > 200)
        pt.y -=  150;
    else
        pt.y -= rc.origin.y;
    [self.tblView setContentOffset:pt animated:YES];

    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

OTHER TIPS

use optimized way like this

selectRowAtIndexPath:animated:scrollPosition:

Selects a row in the receiver identified by index path, optionally scrolling the row to a location in the receiver.

- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition

Special Considerations Passing UITableViewScrollPositionNone will result in no scrolling, rather than the minimum scrolling described for that constant. To scroll to the newly selected row with minimum scrolling, select the row using this method with UITableViewScrollPositionNone, then call scrollToRowAtIndexPath:atScrollPosition:animated: with UITableViewScrollPositionNone.

NSIndexPath *rowToSelect;  // assume this exists and is set properly
UITableView *myTableView;  // assume this exists

[myTableView selectRowAtIndexPath:rowToSelect animated:YES scrollPosition:UITableViewScrollPositionNone];
[myTableView scrollToRowAtIndexPath:rowToSelect atScrollPosition:UITableViewScrollPositionNone animated:YES];

more understand Setting scroll position in UITableView

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top