问题描述
|
我有一个比窗口短的
UITableView
,因此不需要滚动。但是,它足够长,以至于当选择底部一行中的文本字段时,键盘将其覆盖。
我不能使用scrollToRowAtIndexPath
,因为表格比窗口短,所以我想知道将其显示在正确的方式上是什么。
我当时正在考虑将整个视图向上滑动一定数量的像素,尽管这看起来非常糟糕,因为如果我向表中添加更多行,则会破坏UI。
解决方法
您应该在相关的类中实现以下方法:
- (void) textFieldDidBeginEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:YES];
}
- (void) textFieldDidEndEditing:(UITextField *)myTextField
{
[self animateTextField:myTextField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
int movement = (up ? -105 : 105);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame,movement);
[UIView commitAnimations];
}
您必须根据情况调整值(-105
,105
和0.3f
)。
, 您可以通过设置footerView的高度来使整个tableView向上滑动。键盘会将表格移至上方,移至页脚的高度
-(CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section
{
return 70.0;
}
, Pierre \的代码对我有用,但是我删除了myTextField参数。似乎没有必要。我也将其更改为UITextViews,因为这就是我的代码。否则,感谢您的问题和答案。为了解决这个问题,我一直在头上撞墙!
#pragma mark - Text View Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
[self animateTextViewUp:YES];
}
- (void) textViewDidEndEditing:(UITextView *)textView
{
[self animateTextViewUp:NO];
}
- (void) animateTextViewUp:(BOOL)up
{
int movement = (up ? -80 :80);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:0.3f];
self.view.frame = CGRectOffset(self.view.frame,movement);
[UIView commitAnimations];
}