如果用户双击文本字段,键盘会变黑

时间:2015-11-12 20:22:14

标签: ios swift uitextfield

我有一个注册页面,其中有3个文本字段。触摸文本字段时视图会向上滑动,以便在键盘显示时可以看到字段。但是,如果用户快速点击两次文本字段,键盘将变为黑色并且视图定位无法挽回,而不是点击一次。这是问题的照片,代码在下面,提前感谢![keyboard is gone, no way to slide the view down to correct position] 1

    override func viewDidLoad() {
        super.viewDidLoad()

        emailTextField.delegate = self
        passwordTextField.delegate = self
        verifyPasswordTextField.delegate = self

        emailTextField.autocorrectionType = UITextAutocorrectionType.No

        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillShow:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide:"), name: UIKeyboardWillHideNotification, object: nil)
    }


// method to move the view up when keyboard appears
func keyboardWillShow(notification: NSNotification) {

       if viewIsLifted == false {

       if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {

                            // hide the logo and name when view slides up
                            UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

                               self.likemojiIcon.alpha = 0
                               self.likemojiLabel.alpha = 0

                               }, completion: nil)

                            self.view.frame.origin.y -= keyboardSize.height
                            viewIsLifted = true
                         }
                      }
                   }


                   // method to slide view back down when keyboard is resigned
                   func keyboardWillHide(notification: NSNotification) {

                      if viewIsLifted == true {

                         if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {

                            // make logo and name reappear on slide back down
                            UIView.animateWithDuration(0.2, delay: 0.0, options: UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in

                               self.likemojiIcon.alpha = 1
                               self.likemojiLabel.alpha = 1

                               }, completion: nil)

                            self.view.frame.origin.y += keyboardSize.height
                            viewIsLifted = false
                         }
                      }
                   }

2 个答案:

答案 0 :(得分:1)

当您收到键盘 的通知时,请关闭用户互动(beginIgnoringInteractionEvents),并在收到通知时将其重新开启(endIgnoringInteractionEvents)键盘出现

答案 1 :(得分:0)

在动画开始之前放置viewIsLifted = true,否则双击可以在变量更改之前注册键盘活动两次,从而崩溃。另外,通知发布在哪里?