Swift:通过NSNotificationCenter的键盘观察器不起作用

4
我正在尝试在我的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
}

奇怪的是,在启动应用程序后,这两个响应键盘的函数都会被调用一次。当我进入或离开文本框时什么也没有发生。我做错了什么?顺便问一下:更改约束是改变图像大小的最佳解决方案吗?

非常感谢您的帮助!


1
我可能错了,但我认为 NSNotificationCenter() 每次调用时都会实例化一个新的 NSNotificationCenter。你尝试过使用 NSNotificationCenter.defaultCenter() 吗? - Patrick Goley
5个回答

8
//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")
}

1
很抱歉要说这个不起作用,因为它是Objective C选择器。#selector(keyboardWillAppear) #selector(keyboardWIllHide) 对我有用。请参考这篇文章:https://dev59.com/9mAg5IYBdhLWcg3wBXER - Werner Kratochwil
当然它能工作,而且确实工作,因此它是最高评分的答案。但是当它用Swift编写时,请说明你的声明“它是Objective C选择器”。 - JSA986
使用给定的代码在XCode中会收到警告:应该使用#selector而不是显式构造'Selector'。此外,println不再有效(我会收到错误),应该替换为print。 - Werner Kratochwil
1
那是因为在回答这个问题的时候(2年前),针对当时Swift的当前版本,答案是正确的。 - JSA986

8

NSNotificationCenter() 的调用会每次实例化一个新的 NSNotificationCenter。尝试使用 NSNotificationCenter.defaultCenter() 替代。


1

使用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")
}

1

Swift 4.0,闭包:

NotificationCenter.default.addObserver(forName: .UIKeyboardDidShow, object: nil, queue: nil, using: { notification in
   // do stuff
})

1

我更喜欢使用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")
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接