2011-05-11 77 views
4

在我的應用程序中,當我單擊文本字段時,鍵盤隱藏它。請幫助我 - 當我點擊文本字段時,如何移動視圖。我在textFieldDidBeginEditing:使用此代碼移動視圖,以便鍵盤不隱藏文本字段

self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 216, 0); 
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 216, 0); 

但它不起作用。

+0

的可能重複[如何使一個的UITextField向上移動時,鍵盤的存在(http://stackoverflow.com/questions/1126726/how-to -make-a-uitextfield-move-up-when-keyboard-is-present) – 2011-05-13 05:54:20

回答

1

你可以做到以下幾點,但首先要確保你已經設置的UITextField代表你的自我和

#define kOFFSET_FOR_KEYBOARD 350; 

在頂部。這是你想要多遠視圖要移位

//method to move the view up/down whenever the keyboard is shown/dismissed 

-(void)setViewMovedUp:(BOOL)movedUp 
{ 
    [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 (movedUp) 
    { 
     // 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]; 
} 


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


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


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


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

- (void)viewWillAppear:(BOOL)animated 
{ 
    [[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]; 
} 

- (void)viewWillDisappear:(BOOL)animated 
{ 
    // unregister for keyboard notifications while not visible. 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; 
} 
+4

這個例子有幾個缺點,基於一些錯誤的假設;從來沒有物理鍵盤,只有肖像模式,蘋果永遠不會改變鍵盤的大小,或動畫的長度。如果用戶希望例如在不先關閉鍵盤的情況下在文本字段之間進行切換,則滾動到被偏移的內容也是不可能的。 – PeyloW 2011-05-11 14:24:59

+0

thanx但我不想移動我的視圖爲所有文本框,所以請告訴我如何選擇特定的文本框 – sandy 2011-05-11 14:30:22

+0

你好,我沒有得到如何防止我的觀點上移鍵盤不隱藏我的文本框。請幫助我 – sandy 2011-05-12 11:10:16

13

你不應該相信textFieldDidBeginEditing:調整爲鍵盤,因爲這種方法將被調用,即使用戶使用物理鍵盤,其中屏幕鍵盤就會打字不被顯示。

取而代之的是聽取UIKeyboardWillShowNotification,這隻有在鍵盤實際顯示時纔會觸發。您需要執行三個步驟:

  1. 從通知userInfo字典中確定鍵盤的實際大小。尺寸將不同於風景/肖像以及不同的設備。
  2. 使用確定的尺寸更新contentInset。你可以做動畫,通知甚至會告訴你鍵盤動畫的持續時間。
  3. 將文本框滾動到視圖中,很容易忘記!

你會發現更多的信息和示例代碼here

+0

請告訴我代碼知道在景觀和potraite鍵盤的大小 – sandy 2011-05-11 14:41:44

+0

@Sandy:完整的代碼,與工作示例可以在我鏈接到答案的developer.apple.com上的鏈接上找到。 – PeyloW 2011-05-11 14:46:03

+0

請記住,Apple示例忘記使用文本框的高度。導致鍵盤邊界上的字段未顯示或未完全顯示。 – ophychius 2012-07-16 11:25:23