简体   繁体   English

从预测键盘切换为简单键盘(iOS 8)时,UIScrollView(包含多个UITextField)正在上下弹跳

[英]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 )在键盘上方可见。

  • I have added the Keyboard notifications ( UIKeyboardWillShowNotification , UIKeyboardWillHideNotification ) in viewWillAppear method. 我在viewWillAppear方法中添加了键盘通知( UIKeyboardWillShowNotificationUIKeyboardWillHideNotification )。
  • I made 'correction' option of textfields tf1, tf3, tf5, tf7 YES , and rest of the textfields to NO ie keyboards with 'YES' to correction option will have predictive bar and others with 'NO' to correction option will not have predictive bar . 我做了文本框的“修正”选项TF1,TF3,TF5,TF 7 为是 ,该文本框的其余NO即键盘与“YES”,以校正选项将预测的酒吧和其他与“否”校正选项将不会有预测吧
  • ScrollView is scrolling very fine and all textfields are visible except one issue that, when predictive bar of the keyboard appears or vanish , the scrollview bounces to the top and then comes to it's right position, which gives weird look. ScrollView滚动非常好,并且所有文本字段都可见,除了一个问题,当键盘的预测栏 出现消失时 ,scrollview会弹到顶部,然后到达正确的位置,这看起来很奇怪。
  • But if I switch off/on the predictive bar of keyboard for all the textfields , then scrollview scrolls smoothly. 但是,如果我为所有文本字段关闭/打开键盘的预测栏,则scrollview会平滑滚动。
  • Please suggest some way to handle the rough scrolling in case of switching from simple keyboard to perdictive_bar keyboard . 如果从简单键盘切换到perdictive_bar键盘,请提出一些处理粗略滚动的方法。
  • Here is the code that I have implemented to do the above process: 这是我为执行上述过程而实现的代码:

pragma mark - Keyboard Notification 实用标记-键盘通知

- (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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM