在文本视图之间切换时,不同的键盘高度会产生奇怪的框架
Different keyboard heights make weird frames when switching between text view
我有一个获取电子邮件和密码的登录屏幕。密码文本字段是安全的,它会导致不同的键盘高度。当我在电子邮件文本字段和密码字段之间切换时,没有先关闭键盘(在显示电子邮件键盘时单击密码),我的框架不会重新计算键盘高度,并且我在两者之间的差异区域得到黑色键盘。
这是在我的视图控制器中处理键盘的代码,很明显,问题是 keyboardWillHide/show
函数没有被再次调用,如果它们没有被关闭,则计算框架的正确高度:
private func setupKeyboard(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification){
UIView.animate(withDuration: 2.0, animations: {
self.appLogo.alpha = 0
})
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= (keyboardSize.height)
}
}
}
func keyboardWillHide(notification: NSNotification){
self.appLogo.alpha = 1
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += (keyboardSize.height)
}
}
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
appLogo.isHidden = false
view.endEditing(true)
}
我正在寻找解决这种情况的方法,也许当我单击密码字段时,它会先关闭电子邮件字段键盘,然后打开密码键盘。
任何帮助表示赞赏。
主要问题是您使用的是相对偏移(在视图的原点上加减),而不是设置绝对值。这与一些不必要的原产地检查相结合,导致了差距。试试这个:
func keyboardWillShow(notification: NSNotification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = -keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = 0
}
}
我有一个获取电子邮件和密码的登录屏幕。密码文本字段是安全的,它会导致不同的键盘高度。当我在电子邮件文本字段和密码字段之间切换时,没有先关闭键盘(在显示电子邮件键盘时单击密码),我的框架不会重新计算键盘高度,并且我在两者之间的差异区域得到黑色键盘。
这是在我的视图控制器中处理键盘的代码,很明显,问题是 keyboardWillHide/show
函数没有被再次调用,如果它们没有被关闭,则计算框架的正确高度:
private func setupKeyboard(){
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")
view.addGestureRecognizer(tap)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func keyboardWillShow(notification: NSNotification){
UIView.animate(withDuration: 2.0, animations: {
self.appLogo.alpha = 0
})
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0{
self.view.frame.origin.y -= (keyboardSize.height)
}
}
}
func keyboardWillHide(notification: NSNotification){
self.appLogo.alpha = 1
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += (keyboardSize.height)
}
}
}
func dismissKeyboard() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
appLogo.isHidden = false
view.endEditing(true)
}
我正在寻找解决这种情况的方法,也许当我单击密码字段时,它会先关闭电子邮件字段键盘,然后打开密码键盘。 任何帮助表示赞赏。
主要问题是您使用的是相对偏移(在视图的原点上加减),而不是设置绝对值。这与一些不必要的原产地检查相结合,导致了差距。试试这个:
func keyboardWillShow(notification: NSNotification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = -keyboardSize.height
}
}
func keyboardWillHide(notification: NSNotification){
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
self.view.frame.origin.y = 0
}
}