我有UIView
UITextField
,可以调出键盘。我在textfield的Delegate方法中使用了UIKeyboardWillShowNotification/UIKeyboardWillHideNotification
。如下图所示:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
self.txtFullName.text=@"";
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
和我的键盘通知方法:
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize;
CGPoint keyboardOrigin;
CGFloat _currentKeyboardHeight = 0.0f;
NSDictionary* keyboardInfo = [notification userInfo];
keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
keyboardOrigin = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].origin;
CGFloat deltaHeight = keyboardSize.height - _currentKeyboardHeight;
_currentKeyboardHeight = keyboardSize.height;
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
CGFloat statusBarHeight = statusBarSize.height;
self.viewForSignature.frame = CGRectMake(self.viewForSignature.frame.origin.x, self.viewForSignature.frame.origin.y+statusBarHeight+self.navigationController.navigationBar.frame.size.height-deltaHeight, self.viewForSignature.frame.size.width, self.viewForSignature.frame.size.height);
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
CGSize keyboardSize;
CGFloat _currentKeyboardHeight = 0.0f;
NSDictionary* keyboardInfo = [notification userInfo];
keyboardSize = [[keyboardInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGFloat deltaHeight = keyboardSize.height - _currentKeyboardHeight;
_currentKeyboardHeight = keyboardSize.height;
CGSize statusBarSize = [[UIApplication sharedApplication] statusBarFrame].size;
CGFloat statusBarHeight = statusBarSize.height;
self.viewForSignature.frame = CGRectMake(self.viewForSignature.frame.origin.x,self.viewForSignature.frame.origin.y-statusBarHeight-self.navigationController.navigationBar.frame.size.height+deltaHeight, self.viewForSignature.frame.size.width, self.viewForSignature.frame.size.height);
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
以下演示的截图:
单击返回键时上下移动键盘,效果很好。但是当点击文本字段并突然点击返回键时查看问题。如下scrrenshot所示:
我该怎么办?提前致谢