ios swift3 键盘高度获取
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
//deinit 的时候remove 下
处理
func keyboardWillShow(_ sender:Notification){
// 获取键盘高度
let userInfo = sender.userInfo
if userInfo == nil{
return
}
let value:NSValue = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
let keyBoardRect = value.cgRectValue
let height = keyBoardRect.size.height
}
func keyboardWillHide(_ sender:Notification){
// 隐藏了. 高度自然为0
}
oc 版本
- (void)viewDidLoad
{
[super viewDidLoad];
//增加监听,当键盘出现或改变时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
//增加监听,当键退出时收出消息
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)aNotification
{
//获取键盘的高度
NSDictionary *userInfo = [aNotification userInfo];
NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [aValue CGRectValue];
int height = keyboardRect.size.height;
}
//当键退出时调用
- (void)keyboardWillHide:(NSNotification *)aNotification{}
//键盘上面view 自动位置
NotificationCenter.default.addObserver(self, selector: #selector(self.keybordFrameChange(_:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keybordHid(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
func keybordHid(_ sender:Notification){
var info = (sender as NSNotification).userInfo //as? NSDictionary
let keyboardDuration = (info?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue ?? 0
self.inputDefaultView.mas_updateConstraints { (make) in
make?.bottom.mas_equalTo()(self.view)
}
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}
func keybordFrameChange(_ sender :Notification){
var keyHeight:CGFloat = 246
var info = (sender as NSNotification).userInfo //as? NSDictionary
let keyboardSize = (info?[UIKeyboardFrameEndUserInfoKey] as AnyObject).cgRectValue //UIKeyboardFrameEndUserInfoKey UIKeyboardFrameBeginUserInfoKey ,, begin的位置会有差距, 结束的最终的才是ok 的
let keyboardDuration = (info?[UIKeyboardAnimationDurationUserInfoKey] as AnyObject).doubleValue ?? 0
if keyboardSize != nil{
keyHeight = (keyboardSize!.height)
}
self.inputDefaultView.mas_updateConstraints { (make) in
make?.bottom.mas_equalTo()(self.view)?.offset()(-keyHeight)
}
UIView.animate(withDuration: keyboardDuration) {
self.view.layoutIfNeeded()
}
}
//原本用这个 但是发现有时候位置不对, 换上面的
func keyBoardWillShow(_ note:Notification)
{
if (note as NSNotification).userInfo == nil{
return
}
let userInfo = (note as NSNotification).userInfo! as NSDictionary
let keyBoardBounds = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
// let keyBoardBoundsRect = self.view.convertRect(keyBoardBounds, toView:nil)
// let keyBaoardViewFrame = inputeView.frame
let deltaY = keyBoardBounds.size.height
self.inputeDefaultView.isHidden = false
self.view.bringSubview(toFront: self.inputeDefaultView)
let animations:(() -> Void) = {
self.inputeDefaultView.transform = CGAffineTransform(translationX: 0,y: -deltaY)
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
}else{
animations()
}
}
func keyBoardWillHide(_ note:Notification)
{
if (note as NSNotification).userInfo == nil{
return
}
let userInfo = (note as NSNotification).userInfo! as NSDictionary
let duration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let animations:(() -> Void) = {
self.inputeDefaultView.transform = CGAffineTransform.identity
}
if duration > 0 {
let options = UIViewAnimationOptions(rawValue: UInt((userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16))
UIView.animate(withDuration: duration, delay: 0, options:options, animations: animations, completion: nil)
UIView.animate(withDuration: duration, delay: 0, options: options, animations: animations, completion: { [weak self](bool) in
if let weakSelf = self{
weakSelf.inputeDefaultView.isHidden = true
}
})
}else{
animations()
}
}
文章评论