我们如何让键盘出现在swift中的textView下面?

我用键盘出现在textfield下面

在View上加载添加观察者()

NSNotificationCenter.defaultCenter().addobserver(self,selector: #selector(Gold_Loan_First_ViewController.keyboardDidShow(_:)),name: UIKeyboardDidShowNotification,object: nil)

    NSNotificationCenter.defaultCenter().addobserver(self,selector: #selector(Gold_Loan_First_ViewController.keyboardWillBeHidden(_:)),name: UIKeyboardWillHideNotification,object: nil)

然后更新框架

weak var activeField: UITextField?

func textFieldDidEndEditing(textField: UITextField) {
    self.activeField = nil


}
func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool {
    if textField==txtOTP {
        txtOTP.errorMessage=""
    }
  return true
}
func textFieldDidBeginEditing(textField: UITextField) {
    self.activeField = textField

}




func keyboardDidShow(notification: NSNotification)
{
    if let activeField = self.activeField,let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        let contentInsets = UIEdgeInsets(top: 0.0,left: 0.0,bottom: keyboardSize.height,right: 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        var aRect = self.view.frame
        aRect.size.height -= keyboardSize.size.height
        if (!CGRectContainsPoint(aRect,activeField.frame.origin)) {
            self.scrollView.scrollRectToVisible(activeField.frame,animated: true)
        }
    }


}

func keyboardWillBeHidden(notification: NSNotification)
{
    let contentInsets = UIEdgeInsetsZero
    self.scrollView.contentInset = contentInsets
    self.scrollView.scrollIndicatorInsets = contentInsets
}

但是我如何为textView做到这一点.我用textView的didBeginEditing尝试了相同的代码,没有任何积极的影响

解决方法

其中一个简单且无代码解决方案是在您的应用中使用以下pod.

IQKeyboardManger

稍后你需要在App Delegate中导入它,并在didfinishLaunching方法添加这两行代码

IQKeyboardManager.sharedManager().enable = true

整个应用程序将解决您的问题.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...