开启监听
[[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 *)noti{
NSLog(@"屏幕高度=====%f",SCREEN_HEIGHT);
NSDictionary *info = [noti userInfo];
NSValue *value = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
CGSize keyboardSize = [value CGRectValue].size;
CGFloat buttomhHeight = self.passwordField.mj_y + self.passwordField.mj_height;
CGFloat keyboardTopHeight = SCREEN_HEIGHT - keyboardSize.height;
NSLog(@"%f %f",buttomhHeight,keyboardTopHeight);
self.distance = buttomhHeight - keyboardTopHeight;
if(self.distance > 0){
self.view.mj_y -= (self.distance+5);
}
}
-(void)keyboardWillHide:(NSNotification *)noti{
if(self.distance > 0){
self.view.mj_y += (self.distance+5);
}
}