2012-04-27 45 views
1

我們用顯示的鍵盤打開一個視圖,但是當後退按鈕被點擊時,視圖從右側滑出,而鍵盤僅在視圖消失時滑動。 如果我們在viewwilldisappear中調用resignFirstResponder,則視圖向右滑動,同時鍵盤向下滑動。 是否可以讓鍵盤滑出視圖?IPhones-讓鍵盤滑出視圖

回答

0

請嘗試在viewDidDisappear方法中粘貼resignFirstresponder

+0

它不起作用。 – gage 2012-04-27 04:28:13

+0

在這裏調用resignfirstresponder,當視圖消失時,視圖滑出並鍵盤滑出,同樣從不會調用resignfirstresponder或將其稱爲ad didunload。 – gage 2012-04-27 04:29:54

0
  **Set notificatins and use these methods.....Hope it solve problem: 

      First of all set your whole view in scrollView** 
      -(void)keyboardDidHide:(NSNotification *)notif 
    { 
      NSTimeInterval duration = 0.4; 
      [UIView animateWithDuration:duration animations: 
       ^{ 
       scrollView.contentSize=CGSizeMake(320,scrollOriginalFrame.size.height); 
       }]; 
       keyboardVisible=NO; 
    } 
    -(void)keyboardDidShow:(NSNotification *)notif 
    { 

     scrollView.contentSize=CGSizeMake(self.view.frame.size.width, scrollOriginalFrame.size.height+235); 
     NSTimeInterval duration = 0.4; 

     [UIView animateWithDuration:duration animations: 
      ^{ 
      [scrollView setContentOffset:CGPointMake(0,162) animated:YES]; 

     }]; 
      keyboardVisible=YES; 
      } 
       **In viewDidLoad() add this** 

     //keyboard 
     scrollOriginalFrame=self.view.frame; 
      scrollOriginalFrame.size.height-=103; 
     scrollView.contentSize=scrollOriginalFrame.size; 
      [[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(keyboardDidShow:) name:UIKeyboardWillShowNotification object:nil]; 
       [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardWillHideNotification object:nil]; 
keyboardVisible=NO; 
+0

看起來我們沒有滾動視圖。而且它似乎也不起作用,keyboardDidHide在鍵盤開始滑動時被調用,此時視圖已經消失。 – gage 2012-04-27 04:58:30

+0

在滾動視圖中移動視圖,並移動滾動視圖......而不是實際視圖 – 2012-04-27 05:40:33

1

還有就是做你想做的沒有標準的方法,但是...
基本上,鍵盤只是一個視圖中,所有其他窗口的頂部在它自己的UIWindow呈現。
因此,理論上,您需要做的是找到鍵盤視圖並將其沿期望的方向移動。我認爲你應該使用transform財產,不要搞砸frame

Class keyboardClass = NSClassFromString(@"UIPeripheralHostView"); 
for (UIWindow *window in [[UIApplication sharedApplication] windows]) { 
    for (UIView *subview in window.subviews) { 
     if ([subview isKindOfClass:keyboardClass]) { 
      // that's keyboard 
     } 
    } 
} 

編輯:
如果你在談論的UINavigationController它推/彈出時的默認幻燈片動畫,然後,你只需要調用resignFirstResponderviewDidDisappearbecomeFirstResponderviewWillAppear你的文本視圖。這樣你的鍵盤就會隨着你的視角一起滑動。

+0

它可以工作,但它仍有其他問題。鍵盤不是向右滑動,而是右下角。當我第二次進入視圖時,鍵盤可以滑入。 – gage 2012-04-27 07:03:33

+0

鍵盤滑落,作爲標準鍵盤解除過程的一部分。我想你在你的viewWillDissapear方法中調用resignFirstResponder。也許你應該觀察鍵盤通知,然後取消它的原始動畫'[view.layer removeAllAnimations]'並執行你自己的動畫。 – 2012-04-27 07:31:33

+0

removeallanimations無法防止鍵盤滑落,似乎系統在鍵盤隱藏後執行鍵盤動畫的計算。 – gage 2012-04-27 09:32:25

1

我已經測試過這個,它適用於iOS 5.1,但是,我不認爲這是推薦的行爲。

for (UIWindow *keyboardWindow in [[UIApplication sharedApplication] windows]) 
    if ([[keyboardWindow description] hasPrefix:@"<UITextEffectsWindow"]) { 
     NSLog(@"%@", [keyboardWindow description]); 
     [UIWindow beginAnimations:@"fadeKeyboard" context:nil]; 
     keyboardWindow.frame = CGRectMake(keyboardWindow.frame.origin.x + keyboardWindow.frame.size.width, keyboardWindow.frame.origin.y, keyboardWindow.frame.size.width, keyboardWindow.frame.size.height); 
     [UIWindow commitAnimations]; 
    } 

您也可以使用通知UIKeyboardWillHideNotification的鍵盤時,會躲起來,或者只是手動使用上面的代碼來檢測。