2013-02-28 102 views
0

所以...我有我的UIScrollView向上移動時,彈出鍵盤,而工作......除了他們的UIScrollView和鍵盤不上來同步...首先,鍵盤彈出,那麼UIScrollView的。如何延遲iOS鍵盤彈出?

我知道有延遲鍵盤,使其在同一時間顯示出來的方式,視圖向上滾動;我怎麼做??我在viewDidLoad中嘗試這樣做:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

...我有這個問題,以及:

- (void)keyboardWillShow:(NSNotification *)notification 
{ 
    [NSTimer scheduledTimerWithTimeInterval:4000 target:self selector:@selector(keyboardWillShow:) userInfo:nil repeats:NO]; 
} 

(我知道,4000是一個龐大的數字,但我想,以確保有一個延遲!!)

此外,當我解僱鍵盤,而不是一個平滑的滾動,UIScrollView只是簡單地跳回原位而不是放鬆下來......有沒有一種合理的方式來照顧呢?


UPDATE:

明白了...感謝史蒂芬費舍爾幫助我在正確的道路上......我感動一切keyboardWillShow,並添加以下代碼:

[UIScrollView beginAnimations:nil context:NULL]; 
[UIScrollView setAnimationDelegate:self]; 
[UIScrollView setAnimationDuration:.32]; 
[UIScrollView setAnimationBeginsFromCurrentState:NO]; 

不知何故,當鍵盤消失時,這也解決了我的「跳躍」問題!哇噢!

+1

你向後工作這一點。您應該將滾動視圖偏移更改爲鍵盤動畫計時。而且很難說爲什麼沒有看到一些相關的代碼就「跳回來」。 – 2013-02-28 03:26:20

+0

好的,那我該怎麼做? – ScatteredFrog 2013-02-28 03:35:09

回答

0

這是蘋果文檔這種情況下給出的代碼。

// 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; 
} 
+0

這就是我使用的代碼,但同樣,鍵盤和scrollview不會同時上升:首先是鍵盤,然後是scrollview。我如何讓他們兩人同時出現? – ScatteredFrog 2013-02-28 03:42:10

+0

勾成'UIKeyboardWillShowNotification',不'UIKeyboardDidShowNotification'。 – 2013-02-28 03:44:12

+0

另外:這不是問題的一部分,但如果你想鴨鍵盤和支持旋轉,你應該確保永遠不會從'viewWillAppear',其中的視圖座標都沒有建立起來表現出來。相反,使用'dispatch_async(dispatch_get_mainqueue(),^ {[field becomeFirstResponder];});'在'viewWillAppear'中。這將確保座標已經更新爲當前的方向,但它仍然看起來無縫。 (如果您不讓設備旋轉,請不要擔心。) – 2013-02-28 03:45:18

0
- (void)viewDidLoad 
    { 

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

- (void)keyboardWasShown:(NSNotification *)notification 
{ 
    // To avoid keyboard hides the view 
    CGRect frame = self.view.bounds; 
if (capitalTextField.enabled ==YES) 
{ 
    if ([notification name]== UIKeyboardDidShowNotification) 
    { 

     frame.origin.y += 200; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
    } 
    else 
    { 
     frame.origin.y -= 200; 
     [self.scrollView scrollRectToVisible:frame animated:YES]; 
    } 
    } 
    } 
0

下面的代碼將延遲彈出

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:0.3f]; 
[textField becomefirstresponder]; 
[UIView commitAnimations];