监听键盘

 1 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
 2 
 3 - (void)dealloc
 4 {
 5     [[NSNotificationCenter defaultCenter] removeObserver:self];
 6     
 7     self.topic.top_cmt = self.savedTopCmt;
 8     self.topic.cellHeight = 0;
 9 }
10 
11 - (void)keyboardWillChangeFrame:(NSNotification *)note
12 {
13     // 修改约束
14     CGFloat keyboardY = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
15     CGFloat screenH = [UIScreen mainScreen].bounds.size.height;
16     self.bottomMargin.constant = screenH - keyboardY;
17     
18     // 执行动画
19     CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
20     [UIView animateWithDuration:duration animations:^{
21         [self.view layoutIfNeeded];
22     }];
23 }

 

posted @ 2016-07-21 18:41  超神船长  阅读(150)  评论(0编辑  收藏  举报