在UIScrollView中关闭UITextField十进制小键盘键盘(Dismiss UITextField decimal pad keyboard in UIScrollView)

编程入门 行业动态 更新时间:2024-10-27 11:28:47
在UIScrollView中关闭UITextField十进制小键盘键盘(Dismiss UITextField decimal pad keyboard in UIScrollView)

我正在为我的问题寻找解决方案,但我发现的所有建议都无法正常工作。

我开始使用基于两个带有十进制小键盘的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() } }

我尝试了不同的解决方案,但没有一个能正常工作。

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:

Thread 1: signal 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() } }

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

更多推荐

本文发布于:2023-07-22 19:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222790.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小键盘   键盘   UITextField   十进制   UIScrollView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!