2013-03-19 75 views
-3

除了頂部文本字段,當我開始鍵入時,iphone鍵盤會隱藏所有剩餘的文本字段。如何在鍵盤上方顯示活動文本字段,然後在隱藏鍵盤時將其恢復正常?如何在iPhone鍵盤顯示時顯示我的活動文本框?

+0

請問你可以在這裏粘貼代碼嗎? – 2013-03-19 07:19:53

+0

彈出虛擬鍵盤時向上移動整個視圖。看到這個。 http://stackoverflow.com/questions/15145341/undock-keyboard-programmatically – 2013-03-19 07:32:57

回答

0

您必須在視圖頂部使用滾動視圖,並根據您選擇的文本字段實施數學contentOffset設置。 您選擇的文本框可以從委託方法獲得和偏移的計算可以用文本框框架

1

手錶UIKeyboardWillShowNotificationUIKeyboardWillHideNotification來完成。隨這些通知提供的NSNotification對象將包含userInfo字典,其中包含有關鍵盤完成動畫後幀所具有的信息。根據這些通知調整您的佈局,以保持您想要被鍵盤覆蓋的東西。

1

在將所有文本字段置於滾動視圖之前,請使用以下代碼。

#define kOFFSET_FOR_KEYBOARD 80.0 

-(void)keyboardWillShow { 
    // Animate the current view out of the way 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)keyboardWillHide { 
    if (self.view.frame.origin.y >= 0) 
    { 
     [self setViewMovedUp:YES]; 
    } 
    else if (self.view.frame.origin.y < 0) 
    { 
     [self setViewMovedUp:NO]; 
    } 
} 

-(void)textFieldDidBeginEditing:(UITextField *)sender 
{ 
    if ([sender isEqual:mailTf]) 
    { 
     //move the main view, so that the keyboard does not hide it. 
     if (self.view.frame.origin.y >= 0) 
     { 
      [self setViewMovedUp:YES]; 
     } 
    } 
} 

//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 

    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. 
     rect.origin.y -= kOFFSET_FOR_KEYBOARD; 
     rect.size.height += kOFFSET_FOR_KEYBOARD; 
    } 
    else 
    { 
     // revert back to the normal state. 
     rect.origin.y += kOFFSET_FOR_KEYBOARD; 
     rect.size.height -= kOFFSET_FOR_KEYBOARD; 
    } 
    self.view.frame = rect; 

    [UIView commitAnimations]; 
} 


- (void)viewWillAppear:(BOOL)animated 
{ 
    // register for keyboard notifications 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(keyboardWillShow) 
              name:UIKeyboardWillShowNotification 
              object:nil]; 

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

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

如果這個代碼太高,不會隱藏textField嗎? – Jsdodgers 2013-03-19 07:26:29

+0

該代碼適用於文本字段,但不適用於位於底部的uitextview.Please help – zzzzz 2013-03-19 07:38:58

+0

是的,如果這在githhub中使用BSKeyboarcontrols – Vinodh 2013-03-19 08:55:18

1

把你UITextFieldUIScrollView和使用這樣的UIScrollViewcontentOffSet屬性:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    if (textField == txt1) // 
    { 
     [txt1 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 20); 
    } 

    else if (textField == txt2) // 
    { 
     [txt1 resignFirstResponder]; 
     [txt2 becomesFirstResponder]; 
     scrMainView.contentOffset = CGPointMake(0, 60); 
    } 
    //And so on.. 
    return YES; 
} 

希望,你會得到答案。

謝謝。

+0

什麼是scrMainView?提到適當的細節,否則用戶不會知道你在暗示什麼。 'scrMainView'可能是scollview的權利? – 2013-03-19 09:45:44

+0

是的。 scrMainView是我的滾動視圖,所以它會將內容偏移量設置得稍微高一點,所以用戶可以輕鬆地進行交互。 – 2013-03-19 09:47:03

+0

然後請提供那些細節。只需看看我的編輯。它會清楚地說明你的答案。 – 2013-03-19 09:48:39