我在子视图中有两个文本字段,当我在子视图和键盘内点击任何文本字段时,我试图移动子视图。问题是当子视图启动时,子视图向下移动,我点击了第二个文本字段。当我触摸除textfields之外的任何视图时它应该隐藏。
我的两个文本域和子视图的定义;
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var loginView: UIView!
viewDidLoad中的代码
NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillHide), name: .UIKeyboardWillShow, object: nil)
另外两个显示和隐藏键盘的功能
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if loginView.frame.origin.y == 0{
let height = keyboardSize.height
self.loginView.frame.origin.y += height
}
}
}
func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if loginView.frame.origin.y != 0 {
let height = keyboardSize.height
self.loginView.frame.origin.y -= height
}
}
}
我想知道当子视图启动时单击文本字段时如何避免子视图下载。
答案 0 :(得分:0)
键盘关闭时,只需将y-origin视图设为零即可。 当键盘打开时点击其他textField,这是。 func keyboardWillHide(通知:NSNotification){
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if loginView.frame.origin.y == 0{
let height = keyboardSize.height
self.loginView.frame.origin.y = -height
}
}
}
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if loginView.frame.origin.y != 0 {
let height = keyboardSize.height
self.loginView.frame.origin.y = 0
}
}
}