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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24999605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:17:40  来源:igfitidea点击:

Swift: Keyboard Observer via NSNotificationCenter doesn't work

iosxcodeswiftios8

提问by stoeffn

I'm trying to implement a simple keyboard observer in my iOS 8 Swift app but it really doesn't work. This is the code im currently using:

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

Strangely both functions to react to the keyboard are called once after starting the app. Nothing happens when I enter or leave a textfield. What am I doing wrong? And by the way: Is altering a constraint the best solution for changing the size of an image?

奇怪的是,在启动应用程序后,对键盘做出反应的两个函数都会被调用一次。当我进入或离开文本字段时没有任何反应。我究竟做错了什么?顺便说一句:改变约束是改变图像大小的最佳解决方案吗?

I really appreciate your help!

我真的很感谢你的帮助!

回答by Patrick Goley

Calling NSNotificationCenter()is instantiating a new NSNotificationCentereach time you call it. Try using NSNotificationCenter.defaultCenter()instead.

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

回答by JSA986

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

回答by schirrmacher

Swift 4.0, closures :

Swift 4.0,闭包:

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

回答by Werner Kratochwil

I prefer to use UIKeyboardWillChangeFrameNotification, because you get informed if the size changes, if e.g. when suggestions are hidden/shown

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

回答by Van

Swift 3 and above code. Added code for getting height for keyboard

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