2011-04-01 113 views
0

我想在用戶開始編輯UITextField並且文本字段被鍵盤隱藏時進行UIScrollView滾動。我正在使用來自以下線程的示例。編輯時無法正常工作時滾動視圖

How to make a UITextField move up when keyboard is present

我有我的四個視UITextFields。當第一次顯示鍵盤時,視圖不會自動滾動。如果我用顯示的鍵盤單擊另一個文本字段,則UIScrollView按照預期滾動。 隱藏鍵盤(通過點擊「完成」按鈕)並再次輕敲UITextField會出現同樣的問題:UIScrollView首先不滾動,但當焦點轉換到另一個文本字段時,它完全滾動。

任何人都可以幫我嗎?

viewDidLoad我設置的滾動視圖

keyboardIsShown = NO; 
CGSize scrollContentSize = CGSizeMake(320, 350); 
self.scrollView.contentSize = scrollContentSize; 

我爲鍵盤通知登錄尺寸在viewWillAppear

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window]; 

然後我註銷在viewWillDisappear

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 

下面的兩個方法由通知調用離子。

- (void)keyboardWillShow:(NSNotification *)n { 
    if (keyboardIsShown) { 
     return; 
    } 

    NSDictionary *userInfo = [n userInfo]; 

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 

    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.size.height -= (keyboardSize.height - 50); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = YES; 
} 

- (void)keyboardWillHide:(NSNotification *)n { 
    NSDictionary *userInfo = [n userInfo]; 

    NSValue *boundsValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; 
    CGSize keyboardSize = [boundsValue CGRectValue].size; 

    CGRect viewFrame = self.scrollView.frame; 
    viewFrame.size.height += (keyboardSize.height - 50); 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    [UIView setAnimationDuration:0.3]; 
    [self.scrollView setFrame:viewFrame]; 
    [UIView commitAnimations]; 

    keyboardIsShown = NO; 
} 
+0

嘿,如果你想在你的「keyboardWillHide:」方法中獲得鍵盤高度,鍵盤的高度是0嗎?你是否在你的方法中設置了一個斷點,以查看程序是否進入該方法? – Lepidopteron 2011-04-01 08:53:44

+0

「keyboardWillHide」中鍵盤的高度爲216.當鍵盤第一次出現時調用'keyboardWillShow',當鍵盤解散時正確調用'keyboardWillHide'。 – simonbs 2011-04-01 09:00:58

回答

1

如果您想在鍵盤可見時顯示textfeild,請使用下面的代碼。不要跟隨滾動視圖。如果強制使用scrollView,則忽略此答案。



#define kOFFSET_FOR_KEYBOARD 280.0 

- (void)keyboardWillHide:(NSNotification *)notif { 
    [self setViewMoveUp:NO]; 
} 


- (void)keyboardWillShow:(NSNotification *)notif{ 
    [self setViewMoveUp:YES]; 
} 


- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    stayup = YES; 
    [self setViewMoveUp:YES]; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField { 
    stayup = NO; 
    [self setViewMoveUp:NO]; 
} 

//method to move the view up/down whenever the keyboard is shown/dismissed 
-(void)setViewMoveUp:(BOOL)moveUp 
{ 
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; // if you want to slide up the view 
    [UIView setAnimationBeginsFromCurrentState:YES]; 

    CGRect rect = self.view.frame; 
    if (moveUp) 
    { 
     // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
     // 2. increase the size of the view so that the area behind the keyboard is covered up. 

     if (rect.origin.y == 0) { 
      rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
      //rect.size.height += kOFFSET_FOR_KEYBOARD; 
     } 

    } 
    else 
    { 
     if (stayup == NO) { 
      rect.origin.y += kOFFSET_FOR_KEYBOARD; 
      //rect.size.height -= kOFFSET_FOR_KEYBOARD; 
     } 
    } 
    self.view.frame = rect; 
    [UIView commitAnimations]; 
} 

試試這種方法。根據您的要求編輯它。

+0

這是非常優雅的,併爲我的特殊情況工作,但不應該調整它滾動到哪裏取決於UITextField在視圖上的位置?但是,謝謝。這太好了:-) – simonbs 2011-04-01 10:24:46

+0

但它不應該調整它滾動的位置,取決於UITextField在視圖上的位置?回覆: - 您應該將#define kOFFSET_FOR_KEYBOARD 280.0更改爲變量並通過這些方法使用該值進行播放。 – 2011-04-01 11:47:52

+0

我解決了這個問題,通過在' - (void)textFieldDidBeginEditing:(UITextField *)textField'中保存選定的文本字段爲'textFieldSelected',然後用'rect.origin.y - = textFieldSelected.frame.origin.y替換'kOFFSET_FOR_KEYBOARD' - kKeyboardOffset;'我的新'kKeyboardOffset'是距離視圖頂部的距離。非常感謝你! – simonbs 2011-04-01 13:14:53