我正在尝试处理屏幕上的键盘外观并移动UIScrollView( tfScroll )来制作我的所有UITextFields( tf1,tf2,tf3,tf4,tf5,tf6,tf7 )键盘上方可见。
- (void)keyboardWillShow: (NSNotification *) noti
{
NSDictionary *info = [noti userInfo];
float kbOffset = [[[noti userInfo] valueForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].origin.y;
CGSize keyboardSize = [info[UIKeyboardFrameEndUserInfoKey]CGRectValue].size;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[info[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[info[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
UIEdgeInsets insets = UIEdgeInsetsMake(tfScroll.contentInset.top, 0, keyboardSize.height+10.0, 0);
tfScroll.contentInset = insets;
tfScroll.scrollIndicatorInsets = insets;
float tfOffset ;
if (tf1.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf1.frame.origin.y + tf1.frame.size.height ;
}
else if (tf2.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf2.frame.origin.y + tf2.frame.size.height ;
}
else if (tf3.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf3.frame.origin.y + tf3.frame.size.height ;
}
else if (tf4.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf4.frame.origin.y + tf4.frame.size.height ;
}
else if (tf5.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf5.frame.origin.y + tf5.frame.size.height ;
}
else if (tf6.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf6.frame.origin.y + tf6.frame.size.height ;
}
else if (tf7.isEditing)
{
tfOffset = tfScroll.frame.origin.y + tf7.frame.origin.y + tf7.frame.size.height ;
}
if ((kbOffset - tfOffset) < 0)
{
CGFloat y = (kbOffset - tfOffset);
tfScroll.contentOffset = CGPointMake(tfScroll.frame.origin.x, y);
}
[UIView commitAnimations];
}
- (void)keyboardWillHide: (NSNotification *) noti
{
UIEdgeInsets insets = UIEdgeInsetsMake(tfScroll.contentInset.top, 0, 0, 0);
tfScroll.contentInset = insets;
tfScroll.scrollIndicatorInsets = insets;
tfScroll.contentOffset = CGPointMake(tfScroll.frame.origin.x, 0);
}
答案 0 :(得分:0)
我在代码中发现了错误。虽然这是一个愚蠢的错误。当我更改“内容偏移量”时UIScrollView将键盘上方隐藏的UITextFields拉出键盘,然后显示&#39;方法,我正在指定负面的“y轴”&#39;内容偏移,导致我的滚动视图中的反弹(因为(kbOffset - tfOffset)的差异为负)。
所以我在上面的代码中做了一些改动,为Scrollview提供+ ve轴偏移,并开始正常运行。
if ((kbOffset - tfOffset) < 0)
{
CGFloat y = (tfOffset - kbOffset);
tfScroll.contentOffset = CGPointMake(tfScroll.frame.origin.x, y);
}