先調用方法為鍵盤彈出收起添加監聽事件
-(void)keyBoardConfig{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardShow:) name:UIKeyboardWillShowNotification? object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardHide:) name:UIKeyboardWillHideNotification object:nil];
}
調用[self keyBoardConfig];
實現監聽方法
#pragma mark-keyboard
//鍵盤收起
-(void)keyBoardHide:(NSNotification *)noti{
//這里獲取鍵盤隱藏時間
CGFloat duration=[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
//隱藏期間要對視圖進行的操作
} completion:nil];
}
//鍵盤彈出
-(void)keyBoardShow:(NSNotification *)noti{
//獲得鍵盤的高度
CGFloat height=[[noti.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat? duration=[[noti.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
[UIView animateWithDuration:duration animations:^{
//鍵盤彈出期間要對視圖進行的操作?
//下面是把視圖據下的約束拖出來 改變值 以達到文本框跟隨鍵盤上升的樣子
//self.buttomConstraint.constant=height;
//[self.view layoutIfNeeded];
} completion:nil];
}
效果圖