第二次单击文本字段时键盘未显示视图
View didn't up by keyboard on second click into text field
我有一个带有登录屏幕(手机和密码)的 iOS 应用程序。让我们想象一个案例:
- 用户点击 phone 数字文本字段,出现数字键盘,视图显示键盘。
- 用户填写 phone 数字并点击密码文本字段,键盘从数字布局更改为标准。
- 用户点击键盘上的 Return 按钮,键盘关闭,视图向下。
- 用户点击 phone 数字文本字段或密码更多时间进行数据更正
在第 4 阶段,键盘出现,但视图不再显示,当我点击键盘上的 return 按钮时,视图下降甚至下降,屏幕顶部出现黑线。我该如何解决这个问题?在代码中我有这个:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
并在 viewDidLoad 中:
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)
和
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height/3
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height/3
}
}
}
试试这个
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height/3
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
}
我建议你使用 IQKeyboardmanager.It 会自动处理这些事情
GitHub link - IQKeyboardManger
我有一个带有登录屏幕(手机和密码)的 iOS 应用程序。让我们想象一个案例:
- 用户点击 phone 数字文本字段,出现数字键盘,视图显示键盘。
- 用户填写 phone 数字并点击密码文本字段,键盘从数字布局更改为标准。
- 用户点击键盘上的 Return 按钮,键盘关闭,视图向下。
- 用户点击 phone 数字文本字段或密码更多时间进行数据更正
在第 4 阶段,键盘出现,但视图不再显示,当我点击键盘上的 return 按钮时,视图下降甚至下降,屏幕顶部出现黑线。我该如何解决这个问题?在代码中我有这个:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
并在 viewDidLoad 中:
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)
和
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height/3
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y += keyboardSize.height/3
}
}
}
试试这个
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y == 0 {
self.view.frame.origin.y -= keyboardSize.height/3
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
}
我建议你使用 IQKeyboardmanager.It 会自动处理这些事情 GitHub link - IQKeyboardManger