美文网首页
IOS弹出和隐藏处理

IOS弹出和隐藏处理

作者: 哔哩哔哩智能喵 | 来源:发表于2016-09-28 10:56 被阅读33次
   //监听点击键盘弹出
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWithShow:) name:UIKeyboardDidShowNotification object:nil];
    //监听键盘隐藏
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWithHidden:) name:UIKeyboardWillHideNotification object:nil];
    
}
#pragma mark - 键盘处理
-(void)keyboardWithHidden:(NSNotification *)note
{
    //键盘回到屏幕下方时修改约束
    self.bottomSpacing.constant = 0;
    //弹出键盘用的时间
    double duration =[note.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    //添加动画效果
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];
}
-(void)keyboardWithShow:(NSNotification *)note
{
    //取出键盘最终的frame
   CGRect rect = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    self.bottomSpacing.constant = rect.size.height;
  double duration =[note.userInfo [UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        [self.view layoutIfNeeded];
    }];

-(void)dealloc
{
    //删除所有通知,在页面销毁的时候需要移除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
}

相关文章

网友评论

      本文标题:IOS弹出和隐藏处理

      本文链接:https://www.haomeiwen.com/subject/mvmhyttx.html