【发布时间】:2012-02-28 21:19:38
【问题描述】:
在我的应用程序中,我有一个 UITableView,其中包含一些自定义单元格,并且在其中放置了 UITextField。 UITextField 有时会被我的键盘挡住,这是一个问题。所以在我的情况下,我试图移动整个 tableview 本身,而不是仅仅滚动到对我来说是个问题的特定单元格。
反正我也遇到这种问题。
无论如何,这是我的 textField 显示方法和结束方法的代码。问题是 UITableView 永远不会移动正确的数量。我只是想将 UITableview 向上移动到足以使当前正在调用的 UITextField 的单元格就在键盘上方。 所以有时它会在我看不到单元格的地方滚动太多,或者整个表格视图在我根本看不到它的地方移动了一个疯狂的量。
这里是我的代码,有没有人发现有什么问题?
- (BOOL)textFieldShouldBeginEditing:(UITextField *)thetextField {
CustomCell *cell = (CustomCell*)[[thetextField superview] superview];
CGRect cellRect = [cell convertRect:cell.frame toView:self.view];
float bottomCell = cellRect.origin.y - cellRect.size.height;
if (bottomCell >= keyboardY) {
subtractionAmount = keyboardY - bottomCell;
didMove = YES;
[UIView animateWithDuration:0.4
animations:^{
thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y + subtractionAmount);
}];
}
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)thetextField {
if (didMove) {
didMove = NO;
[UIView animateWithDuration:0.4
animations:^{
thetableView.center = CGPointMake(thetableView.center.x, thetableView.center.y - subtractionAmount);
}];
}
谢谢!
【问题讨论】:
标签: ios uitableview keyboard uitextfield frame