在 swift 中启用时在键盘上方显示按钮时出现问题
Issue while showing button on above keyboard when enable in swift
我试图在启用时在键盘上方显示一个按钮,但它不能正常工作,当键盘打开时按钮不会移动到键盘上方,当我隐藏键盘时它会移动到屏幕顶部,当它显示给用户时,我如何在键盘上方显示按钮。这是我显示按钮的代码,
NotificationCenter.default.addObserver(self, selector: #selector(SignUpVC.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(SignUpVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.nextBtn.frame.origin.y == 0 {
self.nextBtn.frame.origin.y -= keyboardSize.height //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.nextBtn.frame.origin.y != 0 {
self.nextBtn.frame.origin.y = 0
}
}
事实上,您应该更改视图的框架而不是更改按钮的框架。将按钮放在视图下方,当键盘弹出时向上推动视图。
Button frame has an initial y value that isn't 0 , so you need to perserve it and set back
选项 1 : 保存按钮框架并恢复它
var btnFrame:CGRect!
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
btnFrame = self.nextBtn.frame
self.nextBtn.frame.origin.y -= keyboardSize.height //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
self.nextBtn.frame = btnFrame
}
选项 2:移动视图本身
@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 //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
Replace UIKeyboardFrameBeginUserInfoKey
with UIKeyboardFrameEndUserInfoKey
我试图在启用时在键盘上方显示一个按钮,但它不能正常工作,当键盘打开时按钮不会移动到键盘上方,当我隐藏键盘时它会移动到屏幕顶部,当它显示给用户时,我如何在键盘上方显示按钮。这是我显示按钮的代码,
NotificationCenter.default.addObserver(self, selector: #selector(SignUpVC.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(SignUpVC.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
if self.nextBtn.frame.origin.y == 0 {
self.nextBtn.frame.origin.y -= keyboardSize.height //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.nextBtn.frame.origin.y != 0 {
self.nextBtn.frame.origin.y = 0
}
}
事实上,您应该更改视图的框架而不是更改按钮的框架。将按钮放在视图下方,当键盘弹出时向上推动视图。
Button frame has an initial y value that isn't 0 , so you need to perserve it and set back
选项 1 : 保存按钮框架并恢复它
var btnFrame:CGRect!
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
btnFrame = self.nextBtn.frame
self.nextBtn.frame.origin.y -= keyboardSize.height //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
@objc func keyboardWillHide(notification: NSNotification) {
self.nextBtn.frame = btnFrame
}
选项 2:移动视图本身
@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 //can adjust as keyboardSize.height-(any number 30 or 40)
}
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if self.view.frame.origin.y != 0 {
self.view.frame.origin.y = 0
}
}
Replace
UIKeyboardFrameBeginUserInfoKey
withUIKeyboardFrameEndUserInfoKey