2013-01-12 25 views
1

我作出了聊天,其功能類似下面的一個:向上移動元素時鍵盤升起

enter image description here

當用戶點擊該消息泡沫,我需要它來提升雙方的UITextField和UITableView(當然是在文本框上方的表格視圖)。同樣,當他們發送或收回信息時,它應該回到原來的狀態。

我試圖解決方案發布here

兩個通知:

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

和實際的功能:

- (void)keyboardWillHideOrShow:(NSNotification *)note 
{ 
    NSDictionary *userInfo = note.userInfo; 
    NSTimeInterval duration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue]; 
    UIViewAnimationCurve curve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]; 

    CGRect keyboardFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; 
    CGRect keyboardFrameForTextField = [self.myTextField.superview convertRect:keyboardFrame fromView:nil]; 

    CGRect newTextFieldFrame = self.myTextField.frame; 
    newTextFieldFrame.origin.y = keyboardFrameForTextField.origin.y - newTextFieldFrame.size.height; 

    [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ 
     self.myTextField.frame = newTextFieldFrame; 
    } completion:nil]; 
} 

但它有兩個問題:

  1. 我不知道如何提高鍵盤上的桌面視圖
  2. 當鍵盤迴落時,輸入框完全消失。
+2

可能的重複:http://stackoverflow.com/questions/9135248/logic-for-moving-text-field-above-keyboard-on-tap/9136698#9136698 –

+0

謝謝,我用你的代碼開始但是有沒有辦法讓變量將文本字段恢復到其原始座標和大小,而不是手動輸入x,y座標? –

+0

是的,只需添加一個實例變量... –

回答

2

這可能有助於....不能把它的信用,但對我的生活我不記得它是從哪裏來的......一個項目幫助我在一個點上,雖然所以這是我用它。

當基於BOOL值YES或NO調用/解除鍵盤時,這將上下移動視圖。這種方式還可以讓您對其他方面有更多的控制權。

- (void) animateTextField: (UITextField*) textField up: (BOOL)up 
{ 
const int movementDistance = 100; 
const float movementDuration = 0.3f; 
int movement = (up ? -movementDistance : movementDistance); 

[UIView beginAnimations: @"anim" context: nil]; 
[UIView setAnimationBeginsFromCurrentState: YES]; 
[UIView setAnimationDuration: movementDuration]; 
self.view.frame = CGRectOffset(self.view.frame, 0, movement); 
[UIView commitAnimations]; 
} 

然後實現它:

-(IBAction)goingup:(id)sender 
{ 
    //Bounces the text field up for editing 
    [self animateTextField: yourtextfield up: YES];} 
} 


-(IBAction)backdown:(id)sender 
{ 
    //Bounces it back when keyboard is dismissed 
    [self animateTextField: yourtextfield up: NO]; 
} 

的動作連接到您的文本字段編輯也開始和編輯沒有結束網點和你設置。

希望有所幫助。

相關問題