1
我在屏幕底部的swift
應用程序中有一個按鈕。它約束是:當我的應用程序中出現鍵盤時,該按鈕部分向上移動(僅該按鈕的文本移動)
我對於從屏幕底部分開我的按鈕的約束也連接插座:
當我運行應用程序時,我看到了我按鈕(我添加了一些背景色,以便我的示例清晰可見):
現在,奇怪的事情發生了 - 當鍵盤顯露,按鈕上的文本向上移動,藍色的背景保持它是:
而且還按鈕的可見部分是不是點擊所有。
它是我的實現中的某種錯誤還是問題?
我的代碼是針對相當簡單:
@IBOutlet weak var continueUsernameBottomConstraint: NSLayoutConstraint!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillAppear), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(tutorialKeyboardWillDisappear), name: .UIKeyboardWillHide, object: nil)
}
func tutorialKeyboardWillAppear(notification: NSNotification){
print("KEYBOARD APPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y
self.view.layoutIfNeeded()
}
func tutorialKeyboardWillDisappear(notification: NSNotification){
print("KEYBOARD DISAPPEARS")
let endFrame = ((notification as NSNotification).userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y
self.view.layoutIfNeeded()
}
continueUsernameBottomConstraint.constant = view.bounds.height - endFrame.origin.y兩個keyboardWillAppear消失不看起來很棒。相反,如果你想在keyboardWillAppear中使用約束:continueUsernameBottomConstraint.constant = continueUsernameBottomConstraint.constant + keyBoardHeight並在willDisappear中繼續使用nameBottomConstraint.constant = continueUsernameBottomConstraint.constant --keyBoardHeight。 – Dili
蘋果公司首選的方法是使用scrollView來顯示鍵盤的情況。或者在這種情況下,即使您可以將整個屏幕向上移動並將鍵盤追加到底部。 – Dili