1
我作出了聊天,其功能類似下面的一個:向上移動元素時鍵盤升起
當用戶點擊該消息泡沫,我需要它來提升雙方的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];
}
但它有兩個問題:
- 我不知道如何提高鍵盤上的桌面視圖
- 當鍵盤迴落時,輸入框完全消失。
可能的重複:http://stackoverflow.com/questions/9135248/logic-for-moving-text-field-above-keyboard-on-tap/9136698#9136698 –
謝謝,我用你的代碼開始但是有沒有辦法讓變量將文本字段恢復到其原始座標和大小,而不是手動輸入x,y座標? –
是的,只需添加一個實例變量... –