2011-08-25 69 views
7

我有一個視圖的應用程序,從視圖的頂部到視圖的底部都有文本字段。我需要它在編輯底部字段時滾動,以便字段可見,但它似乎不能正常工作。鍵盤在活動文本字段上滾動 - 滾動到視圖外?

Apple docs後,我把所有的代碼放到我的程序中(清單4-1,4-2),並將scrollViewactiveField插座添加到我的頭文件並將它們鏈接到IB。

問題是,只要我點擊一個文本字段,所有的文本字段都會熄滅,直到我關閉鍵盤。他們向下滾動很遠(再次,遠遠沒有任何字段可見)。

有誰知道可能會導致什麼問題?

我將代碼放在Apple Docs的這裏,以便您可以確切地看到我使用的代碼,而無需點擊。

//my .h 
    IBOutlet UIScrollView *scrollView; 
    IBOutlet UITextField *activeField; 

//.m 
    // Call this method somewhere in your view controller setup code. 
    - (void)registerForKeyboardNotifications 
    { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(keyboardWasShown:) 
       name:UIKeyboardDidShowNotification object:nil]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
      selector:@selector(keyboardWillBeHidden:) 
      name:UIKeyboardWillHideNotification object:nil]; 

} 

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(NSNotification*)aNotification 
{ 
    NSDictionary* info = [aNotification userInfo]; 
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0); 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 

    // If active text field is hidden by keyboard, scroll it so it's visible 
    // Your application might not need or want this behavior. 
    CGRect aRect = self.view.frame; 
    aRect.size.height -= kbSize.height; 
    if (!CGRectContainsPoint(aRect, activeField.frame.origin)) { 
     CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height); 
     [scrollView setContentOffset:scrollPoint animated:YES]; 
    } 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    scrollView.contentInset = contentInsets; 
    scrollView.scrollIndicatorInsets = contentInsets; 
} 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
} 

- (void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    activeField = nil; 
} 
+0

我編輯的蘋果代碼的拷貝 - 粘貼了您的文章,因爲它是值得懷疑的SO具有從蘋果授權複製它。我還標示讓主持人看一看 - 我不確定官方政策是或應該是什麼。 –

+0

@Josh:對於[[合理使用]](http://en.wikipedia.org/wiki/Fair_use)目的而言,少量代碼可能是正確的。 –

+0

@羅伯特:好的,謝謝。詹姆斯,我想這意味着如果你願意,你應該把它放回去。對不起,麻煩了。 –

回答

33

UPDATE:下面的答案是過時的。現在您可以使用「TPkeyboardavoiding」庫來處理UIScrollView中所有類型的文本字段操作,UITableView &還有更多。嘗試一下,讓我知道是否有人在整合方面有問題。

老回答

沒有必要爲鍵盤通知此功能寄存器。爲了居中鍵盤在屏幕上方的活躍文本框,你只需要設置的UIScrollView兩種方法的contentOffset如下所示:

// called when textField start editting. 
- (void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    activeField = textField; 
    [scrollView setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES]; 
} 



// called when click on the retun button. 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 


    NSInteger nextTag = textField.tag + 1; 
    // Try to find next responder 
    UIResponder *nextResponder = [textField.superview viewWithTag:nextTag]; 

    if (nextResponder) { 
     [scrollview setContentOffset:CGPointMake(0,textField.center.y-60) animated:YES]; 
     // Found next responder, so set it. 
     [nextResponder becomeFirstResponder]; 
    } else { 
     [scrollview setContentOffset:CGPointMake(0,0) animated:YES]; 
     [textField resignFirstResponder]; 
     return YES; 
    } 

    return NO; 
} 

注:所有文本字段應該有增量標籤,如1,2, 3沒有。並設置代表自我。

感謝,

+5

如果文本字段不直接包含滾動視圖「center.y」將是錯誤的位置。因此,應該使用類似這樣的東西來計算正確的位置:'_scrollView.contentOffset = CGPointMake(0,[_scrollView convertPoint:CGPointZero fromView:textField] .y - 60);' – AndiDog

+0

什麼是-60值? –

+0

-60是根據您的視圖和鍵盤大小而變化的偏移量。在我看來,這個價值最適合我。 – MinuMaster

0

確保委託成立後,我通過代碼去了,掏出所有的一切的座標,並做了所有的數學,不得不手動設置一些變數,但現在按預期工作。

如果有人在將來需要幫助,我會很樂意提供幫助。我的Twitter是在我的個人資料中。

+0

嗨,詹姆斯,我遇到了一個問題以及遵循蘋果的文檔。我的問題是當我點擊文本字段時,沒有任何反應。滾動條不會顯示,也不會滾動。你能幫我一下嗎?提前致謝。 – dumbfingers

+0

對不起,反應慢。一直忙於工作。你有一些我可以看的代碼嗎?如果您使用IB,請確保您的所有插座都已連接好。 – Baub

+0

嗨,詹姆斯,感謝您的回覆,我幾個小時後才問你,我自己解決了這個問題。不管怎麼說,還是要謝謝你!!! – dumbfingers

6

因爲我發現,我用TPKeyboardAvoiding

這是偉大的工作,而且非常易於安裝:

  1. 添加一個UIScrollView到您的視圖控制器的XIB
  2. 設置滾動視圖類到TPKeyboardAvoidingScrollView(通過身份檢查器在xib中仍爲 )
  3. 將所有控件放置在該滾動視圖中

它還會自動掛上鍵盤上的「下一步」按鈕以切換文本字段。

祝你好運!

1

@ Minumaster的方法工作正常,但我想建議一個更簡單但通用的方法,就像蘋果所做的那樣,將鍵盤的高度考慮在內,這對我們在鍵盤上使用自定義工具欄時非常有幫助。 雖然蘋果的做法here已經沒有幾個問題。

這裏是我的方法(稍加修改蘋果的方式) -

// Called when the UIKeyboardDidShowNotification is sent. 
- (void)keyboardWasShown:(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; 
} 

// Called when the UIKeyboardWillHideNotification is sent 
- (void)keyboardWillBeHidden:(NSNotification*)aNotification 
{ 
    UIEdgeInsets contentInsets = UIEdgeInsetsZero; 
    self.scrollView.contentInset = contentInsets; 
    self.scrollView.scrollIndicatorInsets = contentInsets; 
}