1、輸入框監聽UIControlEventEditingDidBegin事件,當用戶開始輸入時,將整個view上移。
輸入框監聽UIControlEventEditingDidEnd事件,當用戶結束輸入時,將整個view下移,恢復到原位置。
[textfield addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingDidBegin];
[textfield addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
通過tag值來確定每個textfield是否響應
2//鍵盤監聽
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(keyboardWillChangeFrame:)
name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
CGRect isUpflag;
[[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&isUpflag];
NSDictionary *info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.25 animations:^{
showView.center = CGPointMake(showView.center.x, self.view.center.y - kbSize.height);
}];
}
kbSize.height可以獲取鍵盤的高度
原文地址 :http://blog.csdn.net/pzhtpf/article/details/9857857