动态监听键盘高度调整文本输入框

来源:互联网 发布:双程网络剧 土豆 编辑:程序博客网 时间:2024/06/06 13:03

 //自定义键盘key事件

    func addCustomKeyboardEvent() {

        NSNotificationCenter.defaultCenter().removeObserver(self)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillChange:", name:UIKeyboardWillChangeFrameNotification, object:nil)

    }

    

    func keyboardWillChange(sender: NSNotification) {

        

        let_ = UITextInputMode().primaryLanguage

        let userInfo: NSDictionary = sender.userInfo!

        //let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

        let keyboardFrame = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue

        

        //let newsCtr = self.childViewControllers[0] as? NewsTableController

            weak var this =self

            UIView.animateWithDuration(0.2, animations: { () ->Void in

                if keyboardFrame.origin.y >self.view.frame.height {

                    this?.comment.frame.origin.y = keyboardFrame.origin.y - 41

                }else {

                    this?.comment.frame.origin.y = keyboardFrame.origin.y - 41

                }

                

                

            })

    }



注意 监听键盘高度的方法是UIKeyboardWillChangeFrameNotification,不要写成别的 ,萌萌大


0 0