NotificationCenter默认addObserver只在充电时起作用?
NotificationCenter default addObserver works only when charging?
我试图在键盘用于显示时向上移动我的文本字段,并在键盘隐藏时再次向下移动。所以我正在使用 NotificationCenter.default.addObserver
但这只有在充电时才能正常工作,而当我从充电器中取出时它不起作用,即它不会移动可能是这种情况。请帮我解决这个问题。提前致谢
下面是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc 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 -= 50
}
}
}
@objc 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 += 50
}
}
}
问题是您使用的是 UIKeyboardFrameBeginUserInfoKey
而不是 UIKeyboardFrameEndUserInfoKey
。
代码应该是:
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
如果 self.view.frame.origin.y
是 0
,则什么也不会发生。对吗?
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
// If `origin.y` is `0`, then do nothing.
if self.view.frame.origin.y != 0{
self.view.frame.origin.y -= 50
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
// If `origin.y` is `0` in `keyboardWillShow`,
// it will still be `0` here.
// So, do nothing.
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += 50
}
}
}
我试图在键盘用于显示时向上移动我的文本字段,并在键盘隐藏时再次向下移动。所以我正在使用 NotificationCenter.default.addObserver
但这只有在充电时才能正常工作,而当我从充电器中取出时它不起作用,即它不会移动可能是这种情况。请帮我解决这个问题。提前致谢
下面是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
@objc 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 -= 50
}
}
}
@objc 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 += 50
}
}
}
问题是您使用的是 UIKeyboardFrameBeginUserInfoKey
而不是 UIKeyboardFrameEndUserInfoKey
。
代码应该是:
@objc func keyboardWillShow(_ notification: Notification) {
if let keyboardFrame: NSValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue {
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
}
}
如果 self.view.frame.origin.y
是 0
,则什么也不会发生。对吗?
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
// If `origin.y` is `0`, then do nothing.
if self.view.frame.origin.y != 0{
self.view.frame.origin.y -= 50
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
// If `origin.y` is `0` in `keyboardWillShow`,
// it will still be `0` here.
// So, do nothing.
if self.view.frame.origin.y != 0{
self.view.frame.origin.y += 50
}
}
}