Iphone 按下新的视图控制器时,TableView框架未正确调整大小,键盘处于隐藏状态
我一定错过了一些基本的东西。我在NavigationViewController中有一个UITableView。在UITableView中选择表行时(使用tableView:DidSelectRowatineXpath:),我调用pushViewController以显示不同的视图控制器。新的视图控制器显示正确,但当我弹出该视图控制器并返回时,UITableView的大小会调整,就像显示键盘一样。在按下视图控制器之前,我需要找到隐藏键盘的方法,以便正确恢复帧。如果我注释掉代码以推送视图控制器,则键盘将正确隐藏,帧大小将正确调整 我用来显示键盘的代码如下:Iphone 按下新的视图控制器时,TableView框架未正确调整大小,键盘处于隐藏状态,iphone,objective-c,uitableview,uiviewcontroller,Iphone,Objective C,Uitableview,Uiviewcontroller,我一定错过了一些基本的东西。我在NavigationViewController中有一个UITableView。在UITableView中选择表行时(使用tableView:DidSelectRowatineXpath:),我调用pushViewController以显示不同的视图控制器。新的视图控制器显示正确,但当我弹出该视图控制器并返回时,UITableView的大小会调整,就像显示键盘一样。在按下视图控制器之前,我需要找到隐藏键盘的方法,以便正确恢复帧。如果我注释掉代码以推送视图控制器,则
- (void) keyboardDidShowNotification:(NSNotification *)inNotification {
NSLog(@"Keyboard Show");
if (keyboardVisible) return;
// We now resize the view accordingly to accomodate the keyboard being visible
keyboardVisible = YES;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.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:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
}
使用以下方法隐藏键盘:
- (void) keyboardWillHideNotification:(NSNotification *)inNotification {
if (!keyboardVisible) return;
NSLog(@"Keyboard Hide");
keyboardVisible = FALSE;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.frame;
tableFrame.size.height += bounds.size.height; // add the keyboard height
if (self.tabBarController != nil) {
tableFrame.size.height -= 48; // subtract the tab bar height
}
tableViewNewEntry.frame = tableFrame;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(_shrinkDidEnd:finished:contextInfo:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
[tableViewNewEntry scrollToNearestSelectedRowAtScrollPosition:UITableViewScrollPositionMiddle animated:YES];
NSLog(@"Keyboard Hide Finished");
}
我通过为视图中的任何第一响应程序控件放弃第一响应程序,触发隐藏键盘。我添加了NSLog语句,并在日志文件中看到了如下情况:
- (void) keyboardDidShowNotification:(NSNotification *)inNotification {
NSLog(@"Keyboard Show");
if (keyboardVisible) return;
// We now resize the view accordingly to accomodate the keyboard being visible
keyboardVisible = YES;
CGRect bounds = [[[inNotification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
bounds = [self.view convertRect:bounds fromView:nil];
CGRect tableFrame = tableViewNewEntry.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:)];
tableViewNewEntry.frame = tableFrame;
[UIView commitAnimations];
}
显示键盘视图将消失:隐藏键盘
隐藏键盘
键盘隐藏完成
PushViewController(在我推送新视图控制器时的NSLog条目)
从这个跟踪中,我可以看到事情以正确的顺序发生,但似乎当按下视图控制器时,键盘隐藏代码没有正确执行 任何想法都将不胜感激。一段时间以来,我一直在用头敲击键盘,试图找出我做错了什么 --添加了DidSelectRowatineXpath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.section) {
case 0: // Required Info
// removed to simplify
case 1: // Optional Info
switch (indexPath.row) {
case 0:
[self showTextDetailPicker: @"Enter a description"
tag: tDescriptionPicker
sourceTarget: self.newRecord
fieldSource: @selector(description)];
break;
default:
break;
}
break;
default:
break;
}
}
- (void) showTextDetailPicker: (NSString*) titleText tag:(int)tagID sourceTarget:(NSObject*)target fieldSource:(SEL)selector{
FieldEditorViewController *fe = [[FieldEditorViewController alloc] init];
fe.titleText = titleText;
fe.fieldText = [target performSelector: selector];
fe.tag = tagID;
// Replace default back button with one that just says 'Back'
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc]
initWithTitle:@"Back"
style:UIButtonTypeInfoLight
target:nil action:nil];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
[fe setDelegate: self];
[self.navigationController pushViewController:fe animated:YES];
[fe release];
}
在按下视图控制器之前,找到第一个响应者并呼叫辞职。使用查看如何递归地查找第一响应者(不确定是否已经这样做)
要记住的另一件事是,您的表视图会自动调整大小,因为您的根视图控制器是从UITableViewController派生的(或者看起来是这样)。如果将根视图控制器设置为包含UITableView的常规UIViewController,则可以更轻松地手动操作表视图的框架,至少这是我的经验。是否可以发布表视图的代码:didSelectRowatineXpath:selector?感谢您的回复。我已经在使用UIView上的a类别来查找并辞退第一响应者。我还使用常规的UIViewController,在显示和隐藏键盘时只调整tableview的大小。Hmmm。很抱歉。我确信这就是问题所在。在-viewwillbeen中调整表视图大小如何?您可以先检查它的当前大小,看看它是否需要调整大小,或者在视图即将出现时强制调整大小?