1铃诬、通過第三方框架管理所有的鍵盤事件
IQKeyboardManager
2、通過監(jiān)聽鍵盤動作來獲取鍵盤
<b>通過NSNotificationCenter監(jiān)聽</b>
UIKeyboardDidShowNotification(鍵盤顯示)
UIKeyboardWillHideNotification(鍵盤消失)
監(jiān)聽到這個動作的時(shí)候苍凛,調(diào)用的方法會有一個NSNotification的參數(shù)
// 監(jiān)聽鍵盤將要顯示的動作
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
// 監(jiān)聽鍵盤將要消失的動作
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
// 監(jiān)聽鍵盤顯示
-(void)keyboardWillShow:(NSNotification*)noti{
//獲取用戶信息
NSDictionary *info = noti.userInfo;
//獲取鍵盤開始的frame
NSValue *heightValue = info[UIKeyboardFrameBeginUserInfoKey];
//將heightValue轉(zhuǎn)化成cgRectValue 在獲取size 在獲取高度
CGFloat height = [heightValue CGRectValue].size.height;
//改變發(fā)送view的高度約束
_viewBottom.constant = height;
//定義存放鍵盤動畫時(shí)間用來讓發(fā)送的view進(jìn)行動畫效果
NSTimeInterval time = 0;
//獲取鍵盤動畫時(shí)長
NSNumber *timeDur = info[UIKeyboardAnimationDurationUserInfoKey];
//把值放給time
[timeDur getValue:&time];
//因?yàn)関iew會改變 所以layoutIfNeeded 刷新布局
[UIView animateWithDuration:time animations:^{
[self.view layoutIfNeeded];
}];
}