[英]UIScrollView (containing number of UITextFields) is bouncing up and down while switching from predictive keyboard to simple keyboard (iOS 8)
I am trying to handle the keyboard appearance on the screen and moving the UIScrollView ( tfScroll ) to make my all UITextFields ( tf1, tf2, tf3, tf4, tf5, tf6, tf7 ) visible above the keyboard. 我正在尝试处理屏幕上的键盘外观,并移动UIScrollView( tfScroll )以使我的所有UITextField( 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);
}
I found the error in my code. 我在代码中发现错误。 It was silly mistake though.
虽然这是愚蠢的错误。 When I was changing the 'content offset' of UIScrollView to pull the hidden UITextFields above the keyboard in 'keyboardWillShow' method, I was assigning negative 'y-axis' to content offset, which causes bounces in my scrollview (as difference of (kbOffset - tfOffset) is negative).
当我更改UIScrollView的``内容偏移量''以在``keyboardWillShow''方法中将隐藏的UITextField拉到键盘上方时,我为内容偏移量分配了负的``y轴'',这导致我的滚动视图出现了反弹(为(kbOffset的差异-tfOffset)为负数。
So I made a little change in above code, provide +ve y-axis displacement to Scrollview, and starts running fine. 所以我在上面的代码中做了一些更改,为Scrollview提供了+ y轴位移,并开始正常运行。
if ((kbOffset - tfOffset) < 0)
{
CGFloat y = (tfOffset - kbOffset);
tfScroll.contentOffset = CGPointMake(tfScroll.frame.origin.x, y);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.