***重要的是时间点 两个通知很容易造成时间点不对
UIKeyboardWillChangeFrameNotification UIKeyboardDidChangeFrameNotification一个是改变完成 另一个是即将改变
添加通知
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardChangeFrame:", name: UIKeyboardWillChangeFrameNotification, object: nil)
移除通知
deinit
{
//移除通知
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillChangeFrameNotification, object: nil)
}
接受到通知后执行的代码:
根据通知的参数userinfo来获取keyboard 移动的frame
func keyboardChangeFrame(noti : NSNotification)
{
guard let userinfo = noti.userInfo else
{
return
}
guard let endFrame = userinfo["UIKeyboardFrameEndUserInfoKey"]?.CGRectValue else
{
return
}
if(endFrame.origin.y == ScreenHeight)
{
//print("回退的时候")
self.toolBar.snp_updateConstraints(closure: { (make) -> Void in
make.bottom.equalTo(self.view.snp_bottom)
})
}
else
{
self.toolBar.snp_updateConstraints(closure: { (make) -> Void in
make.bottom.equalTo(self.view.snp_bottom).offset(-endFrame.size.height)
})
// print("弹出的时候")
}
UIView.animateWithDuration(0.25) { () -> Void in
self.toolBar.layoutIfNeeded()
}
}