2015-03-19 58 views
0

我試圖處理屏幕上的鍵盤外觀並移動UIScrollView(tfScroll)以使我的所有UITextFields(tf1,tf2,tf3,tf4,tf5,tf6,tf7)在鍵盤上方可見。從預測鍵盤切換到簡單鍵盤(iOS 8)時,UIScrollView(包含多個UITextFields)上下跳動

  • 我已經添加了鍵盤通知 (UIKeyboardWillShowNotificationUIKeyboardWillHideNotification)在 viewWillAppear中方法。
  • 我做「修正」文本框TF1,TF3,TF5的選項,TF 7 YES,和文本框的其餘NO即鍵盤與「YES」,以校正選項將預測吧和其他'沒有'更正選項將不會有預測條
  • 滾動型的滾動非常精細,所有文本框都可見 除了一個問題,當鍵盤 預測欄出現消失,滾動視圖反彈的頂部,然後涉及到它的正確位置,這看起來很奇怪。
  • 但是,如果我關閉所有文本框的鍵盤的預測欄,然後 滾動視圖順利滾動。
  • 請建議一些方法來從簡單的鍵盤處理在 切換情況下,粗糙滾動到perdictive_bar鍵盤
  • 這裏是我已經實現做上述過程的代碼:

編譯馬克 - 我發現錯誤在我的代碼鍵盤通知

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

。這是愚蠢的錯誤,但。當我改變UIScrollView的'內容偏移量'以在'keyboardWillShow'方法中將隱藏的UITextFields拉到鍵盤上方時,我將負數'y軸'分配給內容偏移量,這導致了我的滾動視圖中的反彈(因爲(kbOffset - tfOffset)爲負)。

因此,我在上面的代碼中做了一點改動,爲scrollview提供了+ y軸位移,並且開始正常運行。

if ((kbOffset - tfOffset) < 0) 
{ 
    CGFloat y = (tfOffset - kbOffset); 

    tfScroll.contentOffset = CGPointMake(tfScroll.frame.origin.x, y); 
}