[RxSwift] 键盘通知


        //监听键盘通知
        NotificationCenter.default.rx
            .notification(UIResponder.keyboardWillChangeFrameNotification)
            .take(until: self.rx.deallocated) //页面销毁自动移除通知监听
            .subscribe(onNext: { [weak self] notification in
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 476}, {375, 336}},
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 812}, {375, 336}},
//                AnyHashable("UIKeyboardFrameEndUserInfoKey"): NSRect: {{0, 812}, {375, 233}},
                guard let `self` = self,
                      let userInfo = notification.userInfo,
                      let value = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue,
                      let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double,
                      let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? UInt
                else { return }
                
                let frame = value.cgRectValue // 键盘 fram
                let intersection = frame.intersection(self.view.frame) // 交集
                let keyboardHeight = intersection.height
                
                UIView.animate(withDuration: duration, delay: 0.0,
                               options: UIView.AnimationOptions(rawValue: curve), animations: {
                    if keyboardHeight > 0 {
                        self.inputBottom.update(inset: keyboardHeight)
                    } else {
                        self.inputBottom.update(inset: self.view.safeAreaInsets.bottom)
                    }
                }, completion: nil)
                
            }).disposed(by: disposeBag)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容