2013-11-23 31 views
0

我有多個文本框的滾動視圖,包括一些在相同的Y位置。我使用下面的代碼來調整滾動視圖的偏移/插入,以便活動文本字段始終可見 - 但是如果用戶已經編輯了一個字段,然後在同一「行」(相同Y位置)上的另一個文本字段上點擊,滾動視圖反彈/重新調整。無論如何要解決這個問題嗎?如何在調整keyboardWillBeShown/keyboardWillBeHidden的內容時防止滾動視圖彈跳

- (void)keyboardWillBeShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 

    UIView *activeField = [self.view findFirstResponder]; 

    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    aRect.size.height -= aRect.origin.y; 
    aRect.origin.y = 0; 

    CGPoint activeFieldOrigin = [activeField convertPoint:CGPointZero toView:self.view.window]; 

    if (!CGRectContainsPoint(aRect, activeFieldOrigin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, activeFieldOrigin.y-kbSize.height); 
     [self.scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
} 

我可以通過執行setContentOffset來防止行爲:animated with animated = NO。所以我認爲,在動畫= YES的情況下,它會將實際的作品踢到某個區塊或延遲執行實際的設置,然後當它/確實/實際執行時,重新顯示會立即將其重置爲相同位置。

想法?

回答

0

您可以存儲scrollview的當前contentoffset,如contentOffsetY = _scrollView.contentOffset.y;然後檢查

if(contentOffsetY==_scrollView.contentOffset.y) 
{ 
//then should not change scrollviews content offset 
} 
else 
{ 
//change content offset 
} 
相關問題