0
点赞
收藏
分享

微信扫一扫

ios swift uitextview如何应对键盘的遮挡

窗外路过了谁 2023-05-07 阅读 89


参考http://stackoverflow.com/questions/36405752/keyboard-to-move-the-view-only-if-the-textfield-is-hidden

1.继承 UITextViewDelegate
2.

NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(TextViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)

3.

func keyboardWillShow(notification:NSNotification) {

            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
                //self.contentTextView.frame.origin.y -= keyboardSize.height
                self.contentTextView.contentInset.bottom = keyboardSize.height

            }

    }

    func keyboardWillHide(notification:NSNotification) {

            if ((notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue) != nil {
                self.contentTextView.contentInset.bottom = 0
                //keyboardIsPresent = false
            }

    }


举报

相关推荐

0 条评论