我正在寻找解决问题的方法,但我发现的所有建议都无效。
我开始使用基于带有小数点的两个TextField
的工作解决方案。要在任何地方点击时解除键盘,考虑到我没有返回键,我成功使用了:
view.endEditing(true)
当我决定添加UIScrollView
以便向上移动屏幕底部的UITextField
时,我的问题就会出现问题。
view.endEditing(true)无法正常工作,因为scrollview正在覆盖视图。我添加了一个TapGestureRecognizer
,除键盘解除外,一切正常。
在TapGestureReconizer
功能下如果我resignFirstResponder
使用activeTextFiled
,应用程序崩溃前往AppDelegate
但错误:
线程1:信号SIGABRT。
class ViewController: UIViewController, UITextFieldDelegate {
var activeField: UITextField?
@IBOutlet weak var textFieldTop: UITextField!
@IBOutlet weak var textFieldBottom: UITextField!
@IBOutlet weak var scrollView: UIScrollView!
func textFieldDidBeginEditing(textField: UITextField) {
activeField = textField
}
func textFieldDidEndEditing(textField: UITextField) {
activeField = nil
}
func registerForKeyboardNotifications() {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden", name: UIKeyboardWillHideNotification, object: nil)
}
func deregisterFromKeyBoardNotification() {
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
}
func keyboardWasShown(notification: NSNotification) {
self.scrollView.scrollEnabled = true
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize!.height + 20, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
var aRect : CGRect = self.view.frame
aRect.size.height -= keyboardSize!.height
if let _ = activeField
{
if (!CGRectContainsPoint(aRect, activeField!.frame.origin))
{
self.scrollView.scrollRectToVisible(activeField!.frame, animated: true)
}
}
self.scrollView.scrollEnabled = false
}
func keyboardWillBeHidden(notification: NSNotification)
{
let info : NSDictionary = notification.userInfo!
let keyboardSize = (info[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue().size
let contentInsets : UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, -keyboardSize!.height, 0.0)
self.scrollView.contentInset = contentInsets
self.scrollView.scrollIndicatorInsets = contentInsets
self.scrollView.scrollEnabled = false
}
override func viewDidLoad() {
super.viewDidLoad()
registerForKeyboardNotifications()
self.textFieldBottom.delegate = self
self.textFieldTop.delegate = self
scrollView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "closeKeyboard"))
}
func closeKeyboard() {
activeField?.resignFirstResponder()
}
}
我尝试了不同的解决方案,但没有一个能够正常运作。
答案 0 :(得分:0)
我仍在寻找代码/故事板中的某个代表或其他可能缺少的部分,但花了额外的时间检查我发现了问题。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
我在原始代码中忘记了“keyboardWillBeHidden:”末尾的“:”。 这引发了异常,因为这个函数有一个参数。
现在一切正常