在输入密码文本期间是否有关于键盘大小更改的通知处理程序?

时间:2019-04-18 02:52:35

标签: ios swift

我已经设置了键盘,以便在键盘出现时切换屏幕。但是当此人尝试输入密码字段时,键盘会更改大小(建议关闭)。由于此更改,当密码字段处于活动状态时,我在底部遇到一个黑条。因此,我需要一种变通办法来根据键盘高度更改屏幕高度。

链接到屏幕截图: https://drive.google.com/file/d/1rPqIdHwtilGPdwrVxTQlDagLwogq3GzT/view?usp=sharing https://drive.google.com/file/d/1Kd7Ppi6ijr1xC15n3hKxdXBblUD0zWMI/view?usp=sharing

func hideKeyboardWhenTappedAround() {
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(UIViewController.dismissKeyboard))
    tap.cancelsTouchesInView = false
    view.addGestureRecognizer(tap)
}
@objc func dismissKeyboard() {
    view.endEditing(true)
}
func keyboardToggle() {
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)

    self.hideKeyboardWhenTappedAround()
}
@objc func keyboardWillShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
        if self.view.frame.origin.y == 0 {
            self.view.frame.origin.y -= (keyboardSize.height)
        }
    }

}

@objc func keyboardWillHide(notification: NSNotification) {
    if self.view.frame.origin.y != 0 {
        self.view.frame.origin.y = 0
    }
}

2 个答案:

答案 0 :(得分:0)

我认为“密码”字段没有特殊的替代方法,但是您可以通过设置窗口颜色来解决黑色问题

UIApplication.shared.keyWindow?.backgroundColor = UIColor.white

答案 1 :(得分:0)

您不需要处理任何其他通知,它应该可以工作。请在您的keyboardWillShow方法中进行一些更改。像这样

  @objc func keyboardWillShow(notification: NSNotification) {

   let info : NSDictionary = notification.userInfo! as NSDictionary

   if let keyboardSize = (info[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.size {. // I've changed 'keyboardFrameBeginUserInfoKey' to UIKeyboardFrameEndUserInfoKey
    // if self.view.frame.origin.y == 0 { I think its not required
        self.view.frame.origin.y -= (keyboardSize.height)
       //}
}

}