点击键盘返回键时,使用UIKeyboardWillShowNotification / UIKeyboardWillHideNotification向上/向下移动视图

时间:2016-05-13 07:56:59

标签: ios objective-c keyboard uitextfield

我有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];
}

以下演示的截图:

enter image description here enter image description here

单击返回键时上下移动键盘,效果很好。但是当点击文本字段并突然点击返回键时查看问题。如下scrrenshot所示:

enter image description here

我该怎么办?提前致谢

0 个答案:

没有答案