views:

1657

answers:

2

I know you can use the "Inset" property in Interface Builder to make a scroll view be inset from the main window so that it doesn't go below existing controls on the screen such as a tab bar, but how can you do this programatically to adjust for when a keyboard is added to the screen? Currently my scroll view has cells under the keyboard that can't be reached because the view is still registering the bottom of the scroll view as the bottom of the phone, not the top of the keyboard. Any help? Thanks!

+1  A: 

Two options. (when you say scroll view i think you mean table view since the title suggests so)

  1. Use a UITableViewController, if the keyboard is added to the table view i believe it will do the resizing for you

  2. In the textfield or textbox protocol in the - (void)textFieldDidBeginEditing:(UITextField *)textField method, resize your table view frame and end did finish editing set it back so (This one you can also do if its a s croll view and not a table view)

    • (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame= tableView.frame; //or scroll view frame.size.height=frame.size.height- keyboardHeight; tableView.frame=frame }

    • (void)textFieldDidEndEditing:(UITextField *)textField { CGRect frame= tableView.frame; //or scroll view frame.size.height=frame.size.height+ keyboardHeight; tableView.frame=frame }

Hope this helps

Daniel
"Use a UITableViewController" was the single most useful hint in all questions regarding this topic.
hop
not really. i think the event for keyboard as stated below or being a delegate of the textfield are both fine and easy to implement...
Daniel
+1  A: 
#pragma mark Keyboard Handling



- (void) viewDidAppear:(BOOL) ani {
    onscreen = YES;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(_keyboardWillAppear:) name:UIKeyboardWillShowNotification object:nil];
    [center addObserver:self selector:@selector(_keyboardDidAppear:) name:UIKeyboardDidShowNotification object:nil];
    [center addObserver:self selector:@selector(_keyboardWillDisappear:) name:UIKeyboardWillHideNotification object:nil];
}

- (void) viewDidDisappear:(BOOL) ani {
    onscreen = NO;

    NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
    [center removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardDidShowNotification object:nil];
    [center removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

- (void) _keyboardWillAppear:(NSNotification*) n {
    NSLog(@"Keyboard is about to appear");
}

- (void) _keyboardDidAppear:(NSNotification*) n {

    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height -= bounds.size.height; // subtract the keyboard height
    if (self.tabBarController != nil) {
        tableFrame.size.height += 48; // add the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(_shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;
    [UIView commitAnimations];

    //[self hideEditorView:currentEditorView];
    //[currentEditorView removeFromSuperview];
}

- (void) _shrinkDidEnd:(NSString*) ident finished:(BOOL) finished contextInfo:(void*) nothing {
    NSIndexPath* sel = [searchResultsTable indexPathForSelectedRow];

    if (![[searchResultsTable indexPathsForVisibleRows] containsObject:sel])
    {
     [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    }
}

- (void) _keyboardWillDisappear:(NSNotification*) n {
    CGRect bounds = [[[n userInfo] objectForKey:UIKeyboardBoundsUserInfoKey] CGRectValue];
    bounds = [self.view convertRect:bounds fromView:nil];

    CGRect tableFrame = searchResultsTable.frame;
    tableFrame.size.height += bounds.size.height; // add the keyboard height

    if (self.tabBarController != nil) {
        tableFrame.size.height -= 48; // subtract the tab bar height
    }

    [UIView beginAnimations:nil context:NULL];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(_shrinkDidEnd:finished:contextInfo:)];
    searchResultsTable.frame = tableFrame;    
    [UIView commitAnimations];

    [searchResultsTable scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
slf
this is an alternative, and i like it because this way the table view does not have to be the delegate of the text fields...+1
Daniel
You shouldn’t use an underscore as a prefix for your methods, because they could collide with Apple’s own methods. You should use something like _slf instead.
Rafael