【发布时间】:2010-04-06 19:19:39
【问题描述】:
我有一个顶部带有工具栏的视图,以及一个在其单元格中带有文本字段的表格视图。当我想编辑放置在表格视图底部的文本字段时,键盘会隐藏正在编辑的文本字段(因为表格视图没有向上滚动)。 我试图实现http://cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html 但这会使我的整个视图向上移动(工具栏和表格视图),而我希望工具栏一直位于视图的顶部。 我把上面提到的代码修改成这样:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
CGRect textFieldRect = [tableView.window convertRect:textField.bounds fromView:textField];
CGRect viewRect = [tableView.window convertRect:tmpTableView.bounds fromView:tableView];
CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height;
CGFloat heightFraction = numerator / denominator;
if (heightFraction < 0.0) {
heightFraction = 0.0;
} else if (heightFraction > 1.0) {
heightFraction = 1.0;
}
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
} else {
animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
}
CGRect viewFrame = tableView.frame;
viewFrame.origin.y -= animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[tableView setFrame:viewFrame];
[UIView commitAnimations];
}
和
- (void)textFieldDidEndEditing:(UITextField *)textField {
CGRect viewFrame = tableView.frame;
viewFrame.origin.y += animatedDistance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[tableView setFrame:viewFrame];
[UIView commitAnimations];
}
这使我的工具栏停留在顶部,但不幸的是表格视图覆盖了工具栏并且工具栏不可见。
有什么办法解决这个问题吗?
【问题讨论】:
标签: iphone