第一步:注册通知:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
第二步:实现方法:
- (void)keyboardWillShow:(NSNotification *)notification
{
NSDictionary * info = [notification userInfo];
NSValue *avalue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [self.view convertRect:[avalue CGRectValue] fromView:nil];
double keyboardHeight=keyboardRect.size.height;//键盘的高度
CGFloat offset = self.view.frame.size.height - ([addressTextFile superview].frame.origin.y + remarksTextView.frame.size.height + keyboardHeight + 50 * heightScale);
if (offset <= 0) {
[UIView animateWithDuration:0.3 animations:^{
CGRect frame = self.view.frame;
frame.origin.y = offset;
self.view.frame = frame;
}];
}
}
- (void)keyboardHide:(UITapGestureRecognizer*)tap{
[nameTextFile resignFirstResponder];
[phoneTextFile resignFirstResponder];
[addressTextFile resignFirstResponder];
[remarksTextView resignFirstResponder];
}
//第三步:移除通知
-(void)viewDidDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
}