只有在某些textField
被選中時,我纔會將視圖向上移動。然而,如果我現在選擇其他textField,它會在其他textField上再次激活,爲什麼?僅當選擇了某個文本字段時纔將視圖向上移動
像這個我處理的移動:
func keyboardWillShow(notification: NSNotification) {
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) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += keyboardSize.height
}
}
}
而且這樣我試圖addObserver
在文本框touchDown
:
@IBAction func didTapCertainTextField(_ sender: Any) {
NotificationCenter.default.addObserver(self, selector: #selector(CreateCardViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
}
流動的話:
可以說我有5個textFields(1,2,3,4,5)
我將觀察者添加到第三個textField。我點擊第一個,視圖不移動,我點擊第三個移動,我再次點擊1,現在它移動。爲什麼?如果點擊的textField爲1,我不希望視圖移動。
@LeoDabus可以的。所以我應該將其添加到textField本身? –