我有一個登錄屏幕,獲取電子郵件和密碼。密碼文本字段是安全的,並且其結果具有不同的鍵盤高度。當我在電子郵件文本字段和密碼字段之間切換時,首先不關閉鍵盤(在提供電子郵件鍵盤時單擊密碼),我的框架不會重新計算鍵盤高度,並且在區域鍵盤。 這是處理鍵盤在我的視圖控制器的代碼,很明顯的問題是,keyboardWillHide/show
FUNC的不被再次調用並計算出正確的高度的框架,如果他們沒有被駁回:不同的鍵盤高度使文本視圖之間切換時奇怪的框架
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)
}
我正在尋找一種方法來解決這種情況,也許當我點擊密碼字段時,它會先解除電子郵件字段的鍵盤,然後打開密碼鍵盤。 任何幫助表示讚賞。
在'keyboardWillHide'爲什麼不把原點設置回0而不是增加鍵盤高度? – paulvs
@paulvs聽起來像一個不錯的方向,不知道我明白如何實現它 – Eyzuky
好吧,這實際上解決了我有一些奇怪的問題,但仍然沒有完全解決問題。謝謝 – Eyzuky