我正在为我的问题寻找解决方案,但我发现的所有建议都无法正常工作。
我开始使用基于两个带有十进制小键盘的TextField的工作解决方案。 在任何地方点击时解除键盘,考虑到我没有返回键,我成功使用:
view.endEditing(true)当我决定添加UIScrollView以便向上移动屏幕底部的UITextField时,我的问题就出现了。
view.endEditing(true)不起作用,因为scrollview覆盖视图。 我添加了一个TapGestureRecognizer ,除键盘关闭以外的所有工作。
在TapGestureReconizer函数下,如果我在resignFirstResponder上使用activeTextFiled ,应用程序崩溃将转到AppDelegate并显示错误:
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() } }线程1:信号SIGABRT。
我尝试了不同的解决方案,但没有一个能正常工作。
I'm looking for a solution to my problem but all the suggestions I've found are not working.
I started with a working solution that is based on two TextField with decimal pad. To dismiss the keyboard when is tapped anywhere, considering i do not have the return key, I used successfully:
view.endEditing(true)My problem stars when I decide to add UIScrollView in order to move up the UITextField on the bottom of the screen.
The view.endEditing(true) is not working because the scrollview is covering view. I add a TapGestureRecognizer and everything work except the keyboard dismiss.
Under the TapGestureReconizer function if I use resignFirstResponder on the activeTextFiled the app crash going to AppDelegate with error:
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() } }Thread 1: signal SIGABRT.
I've tried different solution but none of them are working.
最满意答案
我仍然期待代码/故事板中的某个代表或其他可能缺失的部分,但花费额外的时间检查我发现问题。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)我在原始代码中忘记了“keyboardWillBeHidden:”末尾的“:”。 这是抛出一个异常,因为这个函数有一个参数。
现在一切正常
I was still looking to some delegate or other possible missing part in the code/storyboard but spending additional hours checking I found the problem.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)I forgot in the original code the ":" at the end of "keyboardWillBeHidden:". This was throwing an exception because this function has an argument.
Now everything is working properly
更多推荐
发布评论