gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:10:27 24 4
gpt4 key购买 nike

我正在尝试在我的 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的函数在启动应用程序后都会被调用一次。当我输入或离开文本字段时没有任何反应。我究竟做错了什么?顺便说一句:更改约束是更改图像大小的最佳解决方案吗?

非常感谢您的帮助!

最佳答案

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

关于ios - Swift:通过 NSNotificationCenter 的键盘观察器不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24999605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com