一、鍵盤出現(xiàn)TableView的高 動態(tài)變化
有時候,當(dāng)我們的鍵盤彈起時候擋住了部分TableView的視圖刑巧,需要讓他的高度發(fā)生變化,變的可以滑動到最低部无畔。
這時候可以通過監(jiān)聽鍵盤的事件來做出相應(yīng)的處理啊楚。
// 監(jiān)聽鍵盤改變
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
我們這里監(jiān)聽的是** UIKeyboardWillChangeFrameNotification**,因為這樣就可以在一個監(jiān)聽事件里處理所有事浑彰,不然寫倆個是很難讓強迫癥接受的事情恭理。
#pragma mark 監(jiān)聽鍵盤事件
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
NSDictionary *info = [notification userInfo];
CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect beginKeyboardRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endKeyboardRect = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat yOffset = endKeyboardRect.origin.y - beginKeyboardRect.origin.y;
[UIView animateWithDuration:duration animations:^{
_tableViewMM.height += yOffset;
}];
}
二、鍵盤出現(xiàn)ToolBarView動態(tài)變化
同樣是上面的通知方法郭变,在監(jiān)聽事件里處理transform
#pragma mark 監(jiān)聽鍵盤事件
- (void)keyboardWillChangeFrame:(NSNotification *)notification{
CGFloat keyboardY = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
CGFloat ty = keyboardY - SCREEN_HEIGHT;
/**
* 像這種移動后又回到原始位置的建議使用transform,因為transform可以直接清零回到原來的位置
*/
[UIView animateWithDuration:duration animations:^{
self.toolBarView.transform = CGAffineTransformMakeTranslation(0, ty);
}];
}
在最后將監(jiān)聽注銷掉
// 移除監(jiān)聽
- (void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}