键盘出现时向上滑动 [英] slide form up when keyboard appears
问题描述
hello我使用表单进行数据输入,
一些文本框位于表单的底部。
当我点击文本字段写入,键盘出现和隐藏字段behing它。如果我使文本字段第一响应者它隐藏键盘,但通过这样做我无法做到这一点。
i想知道当键盘出现时,整个表单应该以我的最后一个字段出现在键盘的op上的方式向上移动
提前感谢
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[self scrollViewToCenterOfScreen:textField];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if([textField isEqual:txtField1])$ b $ b {
[txtField2 becomeFirstResponder];
}
else if([textField isEqual:txtField2])
{
[txtField3 becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0,0)animated:YES];
}
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; //删除键盘覆盖的区域
CGFloat y = viewCenterY - availableHeight / 2.0;
if(y< 0){
y = 0;
}
[scrollView setContentOffset:CGPointMake(0,y)animated:YES];
}
hello i am using a form for data entry , some of the textfields are in the bottom of form. when i click on text fields for writing, keyboard appears and hides the fields behing it. if i make textfield first responder it hides keyboard but by doing this i am unable to do this. i want to know how it is possible that when keyboard appears, whole form should move up in the way that my last field is appearing on the op of key board thanks in advance
Add all of your view in a scroll view and set delegate of scroll view and textfield then use these delegate and method -
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[self scrollViewToCenterOfScreen:textField];
return YES;
}
-(BOOL) textFieldShouldReturn:(UITextField *)textField{
if ([textField isEqual:txtField1])
{
[txtField2 becomeFirstResponder];
}
else if ([textField isEqual:txtField2])
{
[txtField3 becomeFirstResponder];
}
else
{
[textField resignFirstResponder];
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
return YES;
}
- (void)scrollViewToCenterOfScreen:(UIView *)theView {
CGFloat viewCenterY = theView.center.y;
CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
CGFloat availableHeight = applicationFrame.size.height - 200; // Remove area covered by keyboard
CGFloat y = viewCenterY - availableHeight / 2.0;
if (y < 0) {
y = 0;
}
[scrollView setContentOffset:CGPointMake(0, y) animated:YES];
}
这篇关于键盘出现时向上滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!