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

5n0oy7gb  于 2022-11-18  发布在  Swift
关注(0)|答案(5)|浏览(129)

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

奇怪的是,这两个对键盘做出React的函数在启动应用程序后都只被调用一次。当我进入或离开文本字段时,什么都没有发生。我做错了什么?顺便问一下:更改约束是否是更改图像大小的最佳解决方案?
我真的很感谢你的帮助!

kb5ga3dv

kb5ga3dv1#

调用NSNotificationCenter()将在每次调用时示例化一个新的NSNotificationCenter。请尝试改用NSNotificationCenter.defaultCenter()

lp0sw83n

lp0sw83n2#

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

yyhrrdl83#

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

iyzzxitl4#

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

vof42yt15#

Swift 4.0,闭合件:

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

相关问题