在UIScrollView中关闭UITextField十进制键盘键盘

时间:2015-10-24 13:21:58

标签: ios uiscrollview keyboard uitextfield dismiss

我正在寻找解决问题的方法,但我发现的所有建议都无效。

我开始使用基于带有小数点的两个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()

    }
}

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

1 个答案:

答案 0 :(得分:0)

我仍在寻找代码/故事板中的某个代表或其他可能缺少的部分,但花了额外的时间检查我发现了问题。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillBeHidden:", name: UIKeyboardWillHideNotification, object: nil)

我在原始代码中忘记了“keyboardWillBeHidden:”末尾的“:”。 这引发了异常,因为这个函数有一个参数。

现在一切正常