如何解决我有一个UITextField,它在被选中时会被键盘隐藏如何看待它?
| 我有一个比窗口短的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];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。