在 UIScrollView 中关闭 UITextField 小数点键盘
Dismiss UITextField decimal pad keyboard in UIScrollView
我正在寻找问题的解决方案,但我发现的所有建议都不起作用。
我从一个基于两个 TextField
和小数位的工作解决方案开始。要在任何地方点击时关闭键盘,考虑到我没有 return 键,我成功使用了:
view.endEditing(true)
当我决定添加 UIScrollView
以便向上移动屏幕底部的 UITextField
时,我的问题就出现了。
view.endEditing(true) 不起作用,因为滚动视图覆盖了视图。我添加了一个 TapGestureRecognizer
,除键盘关闭外,一切正常。
在 TapGestureReconizer
函数下,如果我在 activeTextFiled
上使用 resignFirstResponder
,应用程序崩溃将转到 AppDelegate
,错误为:
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()
}
}
我尝试了不同的解决方案,但 none 的解决方案有效。
我仍在寻找 code/storyboard 中的某些代表或其他可能遗漏的部分,但花了额外的时间检查我发现了问题。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
我在原代码中忘记了 "keyboardWillBeHidden:" 末尾的“:”。
这是抛出一个异常,因为这个函数有一个参数。
现在一切正常
我正在寻找问题的解决方案,但我发现的所有建议都不起作用。
我从一个基于两个 TextField
和小数位的工作解决方案开始。要在任何地方点击时关闭键盘,考虑到我没有 return 键,我成功使用了:
view.endEditing(true)
当我决定添加 UIScrollView
以便向上移动屏幕底部的 UITextField
时,我的问题就出现了。
view.endEditing(true) 不起作用,因为滚动视图覆盖了视图。我添加了一个 TapGestureRecognizer
,除键盘关闭外,一切正常。
在 TapGestureReconizer
函数下,如果我在 activeTextFiled
上使用 resignFirstResponder
,应用程序崩溃将转到 AppDelegate
,错误为:
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()
}
}
我尝试了不同的解决方案,但 none 的解决方案有效。
我仍在寻找 code/storyboard 中的某些代表或其他可能遗漏的部分,但花了额外的时间检查我发现了问题。
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)
我在原代码中忘记了 "keyboardWillBeHidden:" 末尾的“:”。 这是抛出一个异常,因为这个函数有一个参数。
现在一切正常