我知道您可以使用Interface Builder中的“Inset”属性使滚动视图从主窗口插入,这样它就不会低于屏幕上的现有控件,例如标签栏,但是你怎么能以编程方式执行此操作以调整键盘何时添加到屏幕?目前我的滚动视图在键盘下方有单元格无法访问,因为视图仍然将滚动视图的底部注册为手机底部,而不是键盘顶部。
答案 0 :(得分:7)
#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];
}
答案 1 :(得分:1)
两个选项。 (当你说滚动视图我认为你的意思是表格视图,因为标题暗示如此)
使用UITableViewController
,如果键盘已添加到表格视图中,我相信它会为您调整大小
在- (void)textFieldDidBeginEditing:(UITextField *)textField
方法的文本字段或文本框协议中,调整表格视图框架的大小,结束完成编辑后将其设置回(如果它是滚动视图而不是的tableView)
(void)textFieldDidBeginEditing:(UITextField *)textField { CGRect frame = tableView.frame; //或滚动视图 frame.size.height = frame.size.height- keyboardHeight; tableView.frame =帧 }
(void)textFieldDidEndEditing:(UITextField *)textField { CGRect frame = tableView.frame; //或滚动视图 frame.size.height = frame.size.height + keyboardHeight; tableView.frame =帧 }
希望这有帮助
答案 2 :(得分:0)
使用Interface Builder或代码的另一种简单方法
将[表格视图大小页脚高度]属性设置为200(对于您的布局,请使用此属性)。这有效地使表格大于其所包含的边界框架,允许表格向上滚动高于正常。
非常适合让用户根据需要上下滚动表格。
答案 3 :(得分:0)