微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我们如何让键盘出现在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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐