Swift:通过 NSNotificationCenter 的键盘观察器不起作用
Posted
技术标签:
【中文标题】Swift:通过 NSNotificationCenter 的键盘观察器不起作用【英文标题】:Swift: Keyboard Observer via NSNotificationCenter doesn't work 【发布时间】:2014-07-28 16:18:12 【问题描述】:我试图在我的 ios 8 Swift 应用程序中实现一个简单的键盘观察器,但它确实不起作用。这是我目前使用的代码:
override func viewDidAppear(animated: Bool)
NSNotificationCenter().addObserver(self, selector: Selector(keyboardWillAppear()), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter().addObserver(self, selector: Selector(keyboardWillHide()), name: UIKeyboardWillHideNotification, object: nil)
override func viewDidDisappear(animated: Bool)
NSNotificationCenter().removeObserver(self)
func keyboardWillAppear()
logoHeightConstraint.constant = 128.0
func keyboardWillHide()
logoHeightConstraint.constant = 256.0
奇怪的是,这两个对键盘做出反应的函数在启动应用程序后都会调用一次。当我进入或离开文本字段时,什么也没有发生。我究竟做错了什么?顺便说一句:改变约束是改变图像大小的最佳解决方案吗?
非常感谢您的帮助!
【问题讨论】:
我可能是错的,但我相信NSNotificationCenter()
每次调用它时都会实例化一个新的 NSNotificationCenter。您是否尝试过使用NSNotificationCenter.defaultCenter()
?
【参考方案1】:
每次调用NSNotificationCenter()
都会实例化一个新的NSNotificationCenter
。尝试改用NSNotificationCenter.defaultCenter()
。
【讨论】:
【参考方案2】://keyboard observers
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillAppear"), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillHide"), name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillAppear()
println("Keyboard appeared")
func keyboardWillHide()
println("Keyboard hidden")
【讨论】:
很抱歉,这不起作用,因为它是一个 Objective C 选择器。 #selector(keyboardWillAppear) #selector(keyboardWIllHide) 对我有用,请参阅这篇文章:***.com/questions/24049020/… 当然可以,并且确实有效,因此它是评分最高的答案。当你用 swift 编写时,请限定你的“它是一个 Objective C 选择器”的声明 在 XCode 中给出的代码得到这个警告:使用 #selector 而不是显式构造一个 'Selector' 而且 println 不再有效(我得到一个错误),应该用 print 替换跨度> 那是因为在回答时(2 年前)它对于当时的 Swift 版本是正确的。【参考方案3】:我更喜欢使用 UIKeyboardWillChangeFrameNotification,因为如果大小发生变化,您会收到通知,例如当建议被隐藏/显示时
//keyboard observers
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillChange), name: UIKeyboardWillChangeFrameNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
func keyboardWillChange(notification:NSNotification)
print("Keyboard size changed")
func keyboardWillHide(notification:NSNotification)
print("Keyboard hidden")
【讨论】:
【参考方案4】:Swift 3 及以上代码。添加了获取键盘高度的代码
func addObservers()
//keyboard observers
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidAppear(notification:)), name: NSNotification.Name.UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardDidHide, object: nil)
@objc func keyboardDidAppear(notification: NSNotification)
print("Keyboard appeared")
let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue.size
print("Keyboard size: \(keyboardSize)")
let height = min(keyboardSize.height, keyboardSize.width)
let width = max(keyboardSize.height, keyboardSize.width)
print(height)
print(width)
@objc func keyboardWillHide(notification: NSNotification)
print("Keyboard hidden")
【讨论】:
【参考方案5】:Swift 4.0,闭包?:
NotificationCenter.default.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil, using: notification in
// do stuff
)
【讨论】:
以上是关于Swift:通过 NSNotificationCenter 的键盘观察器不起作用的主要内容,如果未能解决你的问题,请参考以下文章