2011-12-22 83 views
0

我有一個帶有多個textFields的scrollView,跟蹤活動字段並確保當彈出鍵盤時它是可見的。這一切都運行良好,但是當我從第三到第四個textField中選中時,我會在textField結束之前在正確的位置出現一些上下「shimmy」。有什麼建議麼?iOS scrollView setContentOffset「shimmy」

-(void)keyboardDidShow:(NSNotification *)notification 
{  
    if (keyboardIsShown)return; 

    NSDictionary* info=[notification userInfo]; 
    // get keyboard size 
    CGSize keyboardSize=[[info objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue].size; 
    //Set scrollview insets to make room for keyboard 
    UIEdgeInsets contentInsets=UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0); 
    scrollView.contentInset=contentInsets; 
    scrollView.scrollIndicatorInsets=contentInsets; 

    //scroll the active text field into view 
    CGRect viewFrame=self.view.frame; 
    viewFrame.size.height-=keyboardSize.height; 
    int fieldHeight=self.currentTextField.bounds.size.height; 
    CGFloat navHeight=self.navigationController.navigationBar.frame.size.height; 
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight); 

    if (!CGRectContainsPoint(viewFrame, viewPoint)) { 
     //scroll to make sure active field is showing 
     CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-keyboardSize.height+navHeight);//+navHeight 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

-(void)showActiveField 
{  
    //this makes sure that activeField shows when selecting another field after initial keyboard show 
    int fieldHeight=self.currentTextField.bounds.size.height; 
    CGPoint viewPoint=CGPointMake(0.0, self.currentTextField.frame.origin.y+fieldHeight); 
    CGRect viewFrame=self.view.frame; 

    int inset=scrollView.contentInset.bottom; 
    if (!CGRectContainsPoint(viewFrame, viewPoint)) { 
     //scroll to make sure active field is showing 
     CGPoint scrollPoint=CGPointMake(0.0, viewPoint.y-inset); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    }  
} 

回答

0

你在哪裏設置keyboardIsShown?你不想在檢查它是否已經設置好之後立即執行該操作嗎?

然後:是第四個字段靠近scrollview的結尾,你有反彈滾動集?

+0

我沒有使用KeyboardIsShown,只是KeyboardDidShow。其實KeyboardDidShow不是問題。不管最初點擊哪個字段,當鍵盤出現時,滾動視圖會順利滾動到正確的位置。接下來點擊第三個字段有這個口吃,從第三個字段到第四個字段(這是第一個被鍵盤隱藏的字段,口吃發生了,下面還有另一個字段,所以我不認爲它是反彈 – mflac 2011-12-23 03:44:21

+0

我剛剛添加了scrollView.bounces = NO ;,但它並沒有影響這個問題 – mflac 2011-12-23 03:49:09

+0

你的代碼說'if(keyboardIsShown)return;'。你在哪裏設置該變量? – 2011-12-23 07:45:24