只有在某些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本身? –