添加观察键盘的通知中心(视图即将出现是添加,即将消失时移除):
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(FamousRoadDetailVC.keyboardWillShow(noti:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(FamousRoadDetailVC.keyboardWillDismiss(noti:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) } override func viewWillDisappear(_ animated: Bool) { super.viewWillDisappear(animated) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil) NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil) }
处理键盘出现和消失时候的高度处理:
/* *键盘即将出现 */ func keyboardWillShow(noti:NSNotification){ isEditingNow = true; if self.keyboardMaskView == nil { self.keyboardMaskView = UIFactory.create_AView(superView: self.view.window, bgColor: UIColor.init(white: 0.01, alpha: 0.01));keyboardMaskView.isUserInteractionEnabled = true self.keyboardMaskView.addGestureRecognizer(UITapGestureRecognizer.init(target: self, action: #selector(FamousRoadDetailVC.tapToDismiss))); self.keyboardMaskView.frame = CGRect.init(x: 0, y: 0, width: Screen_Width, height: 0); } let info = noti.userInfo; //let keyboardValue : NSValue = info![UIKeyboardFrameBeginUserInfoKey] as! NSValue let keyboardValue : NSValue = info![UIKeyboardFrameEndUserInfoKey] as! NSValue let keyboardBounds = keyboardValue.cgRectValue let durationValue = (info![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue let deltaY = keyboardBounds.size.height; print("======\(deltaY)"); self.keyboardMaskView.frame = CGRect.init(x: 0, y: 0, width: Screen_Width, height: Screen_Height-44-deltaY); //-64 //self.keyboardMaskView.backgroundColor = UIColor.purple let animations:(() -> Void) = { //self.keyboardView.transform = CGAffineTransform(translationX: 0,y: -deltaY) self.keyboardView.frame = CGRect.init(x: 0, y: kScreenHeight-deltaY-64-44, width: Screen_Width, height: 44) } if durationValue > 0 { let options = UIViewAnimationOptions(rawValue: UInt((info![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber).intValue << 16)) UIView.animate(withDuration: durationValue, delay: 0, options:options, animations: animations, completion: nil) }else{ animations() } } //键盘即将消失 func keyboardWillDismiss(noti:NSNotification){ if self.keyboardMaskView != nil { self.keyboardMaskView.removeFromSuperview() self.keyboardMaskView = nil };isEditingNow = false; let userInfo = noti.userInfo let duration = (userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue let animations:(() -> Void) = { //self.keyboardView.transform = CGAffineTransform.identity self.keyboardView.frame = CGRect.init(x: 0, y: kScreenHeight-64-44, width: Screen_Width, height: 44) } 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() } }
这里着重要记录下来的是:
//let keyboardValue : NSValue = info![UIKeyboardFrameBeginUserInfoKey] as! NSValue let keyboardValue : NSValue = info![UIKeyboardFrameEndUserInfoKey] as! NSValue
我在做项目过程中处理键盘高度,因为UIKeyboardFrameBeginUserInfoKey
和 UIKeyboardFrameEndUserInfoKey
的触发时机不同,处理高度得到想要效果上费过时间。所以先记录下来。以后再详细探讨。