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!
views:
1657answers:
2Two options. (when you say scroll view i think you mean table view since the title suggests so)
Use a UITableViewController, if the keyboard is added to the table view i believe it will do the resizing for you
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
#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];
}