我用键盘出现在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尝试了相同的代码,没有任何积极的影响